Update app.py
Browse files518 unmodified lines5195205215225235245256 unmodified lines5325335345355365375385395405415425435445455465471 unmodified line5495505515525535545555565575585595605615625635645655665675685695705715725731 unmodified line5755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126132 unmodified lines6166176186196206216226236246256266276286296306316326336346356366376386396406413 unmodified lines645646647648649650651652653654655518 unmodified linestitle_size = settings.get("title_size", 48)manim_code = f"""from manim import *class GeneratedMathScene(Scene):def construct(self):# Scene settings6 unmodified linesequation_size = {equation_size}title_size = {title_size}wrap_width = {wrap_width}def make_wrapped_paragraph(content, color, font, font_size, line_spacing=0.2):lines = []words = content.split()current = ""for w in words:test = w if not current else current + " " + wtest_obj = Text(test, color=color, font=font, font_size=font_size)if test_obj.width <= wrap_width * 0.9:current = testelse:1 unmodified lineline_obj = Text(current, color=color, font=font, font_size=font_size)lines.append(line_obj)current = wif current:lines.append(Text(current, color=color, font=font, font_size=font_size))if not lines:return VGroup()first_line = lines[0]for ln in lines:ln.align_to(first_line, LEFT)para = VGroup(*lines).arrange(DOWN, aligned_edge=LEFT, buff=line_spacing)return paracontent_group = VGroup()current_y = 3.0line_spacing = 0.8slides = {slides_repr}for idx, slide in enumerate(slides):obj = Nonecontent = slide.get("content", "")1 unmodified linescalelen = slide.get("duration", 1.0)duration = scalelen * {scale}slide_type = slide.get("type", "text")if slide_type == "title":title_text = contentif title_text:lines_group = make_wrapped_paragraph(title_text, highlight_color, default_font, title_size, line_spacing=0.3)obj = lines_group if len(lines_group) > 0 else Text(title_text, color=highlight_color, font=default_font, font_size=title_size)else:obj = Text("", color=highlight_color, font=default_font, font_size=title_size)if obj.width > wrap_width:obj.scale_to_fit_width(wrap_width)obj.move_to(ORIGIN)self.play(FadeIn(obj), run_time=duration * 0.8)self.wait(duration * 0.3)self.play(FadeOut(obj), run_time=duration * 0.3)continueelif slide_type == "text":parts = content.split("#")inline_items = []for p in parts:if not p.strip():continue # ignore emptyif p.startswith("%"):eq = p[1:] # remove %eq_obj = MathTex(eq, color=default_color, font_size=equation_size)inline_items.append(eq_obj)else:text_obj = make_wrapped_paragraph(p, default_color, default_font, text_size)inline_items.append(text_obj)obj = VGroup(*inline_items).arrange(RIGHT, aligned_edge=LEFT, buff=0.15)elif slide_type == "equation":eq_content = contenttest = MathTex(eq_content, color=default_color, font_size=equation_size)2 unmodified linesmid = len(parts) // 2line1 = " ".join(parts[:mid])line2 = " ".join(parts[mid:])wrapped_eq = f"{{{{line1}}}} \\\\ {{{{line2}}}}"obj = MathTex(wrapped_eq, color=default_color, font_size=equation_size)else:obj = MathTex(eq_content, color=default_color, font_size=equation_size)if obj.width > wrap_width:obj.scale_to_fit_width(wrap_width)if obj:obj.to_edge(LEFT, buff=0.3)obj.shift(UP * (current_y - obj.height / 2))obj_bottom = obj.get_bottom()[1]if obj_bottom < -3.5:scroll_amount = abs(obj_bottom - (-3.5)) + 0.3self.play(content_group.animate.shift(UP * scroll_amount), run_time=0.5)current_y += scroll_amountobj.shift(UP * scroll_amount)obj.to_edge(LEFT, buff=0.3)if animation == "write_left":self.play(Write(obj), run_time=duration)elif animation == "fade_in":3 unmodified linesself.play(obj.animate.set_color(highlight_color), run_time=duration * 0.4)else:self.play(Write(obj), run_time=duration)content_group.add(obj)current_y -= (getattr(obj, "height", 0) + line_spacing)self.wait(0.3)if len(content_group) > 0:final_box = SurroundingRectangle(content_group[-1], color=highlight_color, buff=0.2)self.play(Create(final_box), run_time=0.8)518 unmodified lines5195205215225235246 unmodified lines5315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755761 unmodified line5785795805815825835845855865875885895905915925935945955965975981 unmodified line6006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336342 unmodified lines6376386396406416426436446456466476486496506516526536546556566576586596606613 unmodified lines665666667668669670671672673674675518 unmodified linestitle_size = settings.get("title_size", 48)manim_code = f"""from manim import *class GeneratedMathScene(Scene):def construct(self):# Scene settings6 unmodified linesequation_size = {equation_size}title_size = {title_size}wrap_width = {wrap_width}def make_inline_segments(content, color, font, text_size, equation_size):if not content:return VGroup()# Split by # separatorsegments = content.split("#")mobjects = []for segment in segments:segment = segment.strip()if not segment:continue# Check if it's a LaTeX equation (starts with %$ and ends with $)if segment.startswith("%$") and segment.endswith("$"):# Remove %$ from start and $ from endlatex_content = segment[2:-1]mob = MathTex(latex_content, color=color, font_size=equation_size)else:# Regular textmob = Text(segment, color=color, font=font, font_size=text_size)mobjects.append(mob)if not mobjects:return VGroup()# Arrange horizontally with minimal spacinginline_group = VGroup(*mobjects).arrange(RIGHT, buff=0.05)return inline_groupdef make_wrapped_paragraph(content, color, font, font_size, line_spacing=0.2):lines = []words = content.split()current = ""for w in words:test = w if not current else current + " " + wtest_obj = Text(test, color=color, font=font, font_size=font_size)if test_obj.width <= wrap_width * 0.9:current = testelse:1 unmodified lineline_obj = Text(current, color=color, font=font, font_size=font_size)lines.append(line_obj)current = wif current:lines.append(Text(current, color=color, font=font, font_size=font_size))if not lines:return VGroup()first_line = lines[0]for ln in lines:ln.align_to(first_line, LEFT)para = VGroup(*lines).arrange(DOWN, aligned_edge=LEFT, buff=line_spacing)return paracontent_group = VGroup()current_y = 3.0line_spacing = 0.8slides = {slides_repr}for idx, slide in enumerate(slides):obj = Nonecontent = slide.get("content", "")1 unmodified linescalelen = slide.get("duration", 1.0)duration = scalelen * {scale}slide_type = slide.get("type", "text")if slide_type == "title":# Use inline segments for titleobj = make_inline_segments(content, highlight_color, default_font, title_size, equation_size)# Fallback to simple text if no inline segmentsif len(obj) == 0:obj = Text(content, color=highlight_color, font=default_font, font_size=title_size)if obj.width > wrap_width:obj.scale_to_fit_width(wrap_width)obj.move_to(ORIGIN)self.play(FadeIn(obj), run_time=duration * 0.8)self.wait(duration * 0.3)self.play(FadeOut(obj), run_time=duration * 0.3)continueelif slide_type == "text":# Use inline segments for textobj = make_inline_segments(content, default_color, default_font, text_size, equation_size)# Fallback if no inline segments detectedif len(obj) == 0:obj = make_wrapped_paragraph(content, default_color, default_font, text_size, line_spacing=0.25)# Handle width overflowif obj.width > wrap_width:obj.scale_to_fit_width(wrap_width)elif slide_type == "equation":eq_content = contenttest = MathTex(eq_content, color=default_color, font_size=equation_size)2 unmodified linesmid = len(parts) // 2line1 = " ".join(parts[:mid])line2 = " ".join(parts[mid:])wrapped_eq = f"{{{line1}}} \\\\ {{{line2}}}"obj = MathTex(wrapped_eq, color=default_color, font_size=equation_size)else:obj = MathTex(eq_content, color=default_color, font_size=equation_size)if obj.width > wrap_width:obj.scale_to_fit_width(wrap_width)if obj:obj.to_edge(LEFT, buff=0.3)obj.shift(UP * (current_y - obj.height / 2))obj_bottom = obj.get_bottom()[1]if obj_bottom < -3.5:scroll_amount = abs(obj_bottom - (-3.5)) + 0.3self.play(content_group.animate.shift(UP * scroll_amount), run_time=0.5)current_y += scroll_amountobj.shift(UP * scroll_amount)obj.to_edge(LEFT, buff=0.3)if animation == "write_left":self.play(Write(obj), run_time=duration)elif animation == "fade_in":3 unmodified linesself.play(obj.animate.set_color(highlight_color), run_time=duration * 0.4)else:self.play(Write(obj), run_time=duration)content_group.add(obj)current_y -= (getattr(obj, "height", 0) + line_spacing)self.wait(0.3)if len(content_group) > 0:final_box = SurroundingRectangle(content_group[-1], color=highlight_color, buff=0.2)self.play(Create(final_box), run_time=0.8)
app.py+61-41