Beispiel #1
0
def demo_highlighting(scene: CodeScene):
    title = PangoText(
        """
        If you want more control, you can create code blocks and
        highlight them manually.
        """,
        font="Helvetica",
        line_spacing=0.5,
    ).scale(0.7)
    scene.play(ShowCreation(title, run_time=3, rate_func=linear))
    scene.wait(2)
    scene.clear()

    scene.add_background(f"{example_dir}/resources/blackboard.jpg")
    tex = scene.create_code(f"{example_dir}/highlights.py")
    scene.play(ShowCreation(tex))
    scene.highlight_line(
        tex,
        11,
        caption=
        "Create code blocks yourself and pass in any arguments the Code class supports to do things "
        "like change the theme or font",
    )
    scene.highlight_lines(
        tex,
        13,
        19,
        caption=
        "Highlight code with a caption to give extra information. A wait is"
        " automatically added for a time based on the length of the caption",
    )
    scene.highlight_line(tex, 21, caption="Reset highlighting and positioning")
    scene.highlight_none(tex)
    scene.play(FadeOut(tex))
    scene.clear()
Beispiel #2
0
def demo_commenting(scene: CodeScene):
    scene.add_background(f"{example_dir}/resources/blackboard.jpg")

    code = scene.animate_code_comments(
        title="examples/commented.py",
        path=f"{example_dir}/commented.py",
        keep_comments=True,
        start_line=6,
        end_line=19,
        reset_at_end=False,
    )

    scene.highlight_line(
        code,
        number=6,
        caption="These caption callouts are "
        "automatically generated from comments when "
        "using animate_code_comments()",
    )
    scene.highlight_lines(
        code,
        start=14,
        end=18,
        caption="You can also highlight multiple "
        "lines by ending the block with '# "
        "end'",
    )
    scene.highlight_none(code)
    scene.play(FadeOut(code))
    scene.clear()
Beispiel #3
0
def demo_sequence(scene: CodeScene):
    title = PangoText(
        """
        You can use Code Video Generator to also illustrate
        high-level concepts through sequence diagrams, or
        if you want more control, your own block diagrams:
        """,
        font="Helvetica",
        line_spacing=0.5,
    ).scale(0.7)
    scene.play(ShowCreation(title, run_time=4, rate_func=linear))
    scene.wait(3)
    scene.clear()

    scene.add_background(f"{example_dir}/resources/blackboard.jpg")
    diagram = SequenceDiagram()
    browser, web, app = diagram.add_objects("Browser", "Web", "App")
    with browser:
        with web.text("Make a request"):
            web.to_target("Do a quick thing", app)
            with app.text("Retrieve a json object"):
                app.to_self("Calls itself")
                app.note("Do lots and lots and lots of thinking")
                app.ret("Value from db")
            web.ret("HTML response")

    diagram.animate(scene)
    scene.wait(3)
    scene.play(FadeOut(diagram))
    scene.clear()
Beispiel #4
0
    def highlight_none(self, code: Code):
        """
        Convenience method for resetting any existing highlighting.

        Args:
            code: The code object, must be wrapped in `AutoScaled`
        """
        if self.caption:
            self.play(FadeOut(self.caption), HighlightNone(code))
            self.caption = None

        self.play(ApplyMethod(code.full_size))
Beispiel #5
0
def demo_sequence(scene: CodeScene):
    title = Text(
        """
        You can use Code Video Generator to also illustrate
        high-level concepts through sequence diagrams, or
        if you want more control, your own block diagrams:
        """,
        font=DEFAULT_FONT,
        line_spacing=0.5,
    ).scale(0.7)
    scene.play(Create(title, run_time=4, rate_func=linear))
    scene.wait(3)
    scene.clear()

    scene.add_background(f"{example_dir}/resources/blackboard.jpg")

    title = Text("examples/sequence-diagrams.py", font=DEFAULT_FONT)
    title.to_edge(UP)
    scene.add(title)

    diagram = AutoScaled(SequenceDiagram())
    browser, web, app = diagram.add_objects("Browser", "Web", "App")

    browser.to(web, "Make a request")
    web.to(app, "Request with no response")
    web.to(app, "Retrieve a json object")
    app.to(app, "Calls itself")
    app.note("Do lots and lots and lots of thinking")
    app.to(web, "Value from db")
    web.to(browser, "HTML response")

    diagram.next_to(title, DOWN)
    scene.play(Create(diagram))
    for interaction in diagram.get_interactions():
        scene.play(Create(interaction))
    scene.wait(3)
    scene.play(FadeOut(diagram),
               *[FadeOut(item) for item in diagram.interactions])
    scene.clear()
Beispiel #6
0
    def animate_code_comments(
        self,
        path: str,
        title: str = None,
        keep_comments: bool = False,
        start_line: int = 1,
        end_line: Optional[int] = None,
        reset_at_end: bool = True,
    ) -> Code:
        """
        Parses a code file, displays it or a section of it, and animates comments

        Args:
            path: The source code file path
            title: The title or file path if not provided
            keep_comments: Whether to keep comments or strip them when displaying
            start_line: The start line number, used for displaying only a partial file
            end_line: The end line number, defaults to the end of the file
            reset_at_end: Whether to reset the code to full screen at the end or not
        """
        code, comments = comment_parser.parse(path,
                                              keep_comments=keep_comments,
                                              start_line=start_line,
                                              end_line=end_line)

        tex = AutoScaled(
            PartialCode(code=code,
                        start_line=start_line,
                        style=self.code_theme))
        if title is None:
            title = path

        title = Text(title, color=WHITE).to_edge(edge=UP)
        self.add(title)
        tex.next_to(title, DOWN)

        self.play(ShowCreation(tex))
        self.wait()

        for comment in comments:
            self.highlight_lines(tex, comment.start, comment.end,
                                 comment.caption)

        if self.caption:
            self.play(FadeOut(self.caption))
            self.caption = None

        if reset_at_end:
            self.play(HighlightNone(tex))
            self.play(ApplyMethod(tex.full_size))
        return tex
Beispiel #7
0
def goodbye(scene: CodeScene):
    title = PangoText(
        """
        Try Code Video Generator today at:

          https://github.com/sleuth-io/code-video-generator

        Thanks for watching!""",
        font="Helvetica",
        line_spacing=0.5,
    ).scale(0.7)
    scene.play(ShowCreation(title, run_time=4, rate_func=linear))
    scene.wait(5)
    scene.play(FadeOut(title))
Beispiel #8
0
    def highlight_lines(self,
                        code: Code,
                        start: int = 1,
                        end: int = -1,
                        caption: Optional[str] = None):
        """
        Convenience method for animating a code object.

        Args:
            code: The code object, must be wrapped in `AutoScaled`
            start: The start line number
            end: The end line number, defaults to the end of the file
            caption: The text to display with the highlight
        """

        if end == -1:
            end = len(code.line_numbers) + code.line_no_from

        layout = ColumnLayout(columns=3)

        actions = []
        if caption and not self.caption:
            self.play(
                ApplyMethod(
                    code.fill_between_x,
                    layout.get_x(1, span=2, direction=LEFT),
                    layout.get_x(1, span=2, direction=RIGHT),
                ))

        if self.caption:
            actions.append(FadeOut(self.caption))
            self.caption = None

        if not caption:
            self.play(ApplyMethod(code.full_size))
        else:
            callout = TextBox(caption,
                              text_attrs=dict(size=0.4, font=DEFAULT_FONT))
            callout.align_to(code.line_numbers[start - code.line_no_from], UP)
            callout.set_x(layout.get_x(3), LEFT)
            actions += [HighlightLines(code, start, end), FadeIn(callout)]
            self.caption = callout

        self.play(*actions)

        if not self.caption:
            self.play(ApplyMethod(code.full_size))
        else:
            wait_time = len(self.caption.text) / (200 * 5 / 60)
            self.wait_until_measure(wait_time, -1.5)
Beispiel #9
0
def demo_render_self(scene: CodeScene):
    scene.add_background(f"{example_dir}/resources/blackboard.jpg")

    # Here is the code rendering this video you are watching now!
    code = scene.animate_code_comments(
        title="examples/intro.py",
        path=f"{example_dir}/intro.py",
        keep_comments=True,
        start_line=92,
        end_line=108,
        reset_at_end=False,
    )
    # end
    scene.wait(2)

    scene.play(FadeOut(code))
    scene.clear()