Code

Qualified name: manim.mobject.text.code\_mobject.Code

class Code(code_file=None, code_string=None, language=None, formatter_style='vim', tab_width=4, add_line_numbers=True, line_numbers_from=1, background='rectangle', background_config=None, paragraph_config=None)[source]

Bases: VMobject

A highlighted source code listing.

Examples

Normal usage:

listing = Code(
    "helloworldcpp.cpp",
    tab_width=4,
    formatter_style="emacs",
    background="window",
    language="cpp",
    background_config={"stroke_color": WHITE},
    paragraph_config={"font": "Noto Sans Mono"},
)

We can also render code passed as a string. As the automatic language detection can be a bit flaky, it is recommended to specify the language explicitly:

Example: CodeFromString

../_images/CodeFromString-1.png
from manim import *

class CodeFromString(Scene):
    def construct(self):
        code = '''from manim import Scene, Square

class FadeInSquare(Scene):
    def construct(self):
        s = Square()
        self.play(FadeIn(s))
        self.play(s.animate.scale(2))
        self.wait()'''

        rendered_code = Code(
            code_string=code,
            language="python",
            background="window",
            background_config={"stroke_color": "maroon"},
        )
        self.add(rendered_code)
class CodeFromString(Scene):
    def construct(self):
        code = '''from manim import Scene, Square

class FadeInSquare(Scene):
    def construct(self):
        s = Square()
        self.play(FadeIn(s))
        self.play(s.animate.scale(2))
        self.wait()'''

        rendered_code = Code(
            code_string=code,
            language="python",
            background="window",
            background_config={"stroke_color": "maroon"},
        )
        self.add(rendered_code)

Parameters:
  • code_file (StrPath | None) – The path to the code file to display.

  • code_string (str | None) – Alternatively, the code string to display.

  • language (str | None) – The programming language of the code. If not specified, it will be guessed from the file extension or the code itself.

  • formatter_style (str | type[Style]) – The style to use for the code highlighting. This can be either the name of a Pygments style or a custom Pygments style class. Defaults to "vim". A list of all available styles can be obtained by calling Code.get_styles_list(); style classes can be retrieved with Code.get_pygments_style().

  • tab_width (int) – The width of a tab character in spaces. Defaults to 4.

  • add_line_numbers (bool) – Whether to display line numbers. Defaults to True.

  • line_numbers_from (int) – The first line number to display. Defaults to 1.

  • background (Literal['rectangle', 'window']) – The type of background to use. Can be either "rectangle" (the default) or "window".

  • background_config (dict[str, Any] | None) – Keyword arguments passed to the background constructor. Default settings are stored in the class attribute default_background_config (which can also be modified directly). If fill_color is not specified, it is taken from the selected formatter_style.

  • paragraph_config (dict[str, Any] | None) – Keyword arguments passed to the constructor of the Paragraph objects holding the code, and the line numbers. Default settings are stored in the class attribute default_paragraph_config (which can also be modified directly). The color setting is ignored because colors are determined by the selected Pygments style.

Notes

Note

The Pygments style controls the colors of the rendered code, including its default foreground, background, and line number colors. To customize the color scheme, pass a custom Pygments style class via formatter_style rather than setting paragraph_config["color"]. See Creating own styles with Pygments for details.

For example, a built-in style can be subclassed without importing its style class directly:

from manim import *
from pygments.token import Comment

BaseStyle = Code.get_pygments_style("vim")

class CustomStyle(BaseStyle):
    background_color = "#1e1e1e"
    line_number_color = "#858585"
    styles = {
        **BaseStyle.styles,
        Comment: "italic #6a9955",
    }


class Example(Scene):
    def construct(self):
        rendered_code = Code(
            code_string="print('Hello, world!')  # greeting",
            language="python",
            formatter_style=CustomStyle,
        )

        self.add(rendered_code)
        self.wait(2)

Methods

get_pygments_style

Return the Pygments style registered under name.

get_styles_list

Get the list of all available formatter styles.

Attributes

always

Call a method on a mobject every frame.

animate

Used to animate the application of any method of self.

animation_overrides

color

default_background_config

default_paragraph_config

depth

The depth of the mobject.

fill_color

If there are multiple colors (for gradient) this returns the first one

height

The height of the mobject.

n_points_per_curve

sheen_factor

stroke_color

width

The width of the mobject.

code

target

original_id

_original__init__(code_file=None, code_string=None, language=None, formatter_style='vim', tab_width=4, add_line_numbers=True, line_numbers_from=1, background='rectangle', background_config=None, paragraph_config=None)

Initialize self. See help(type(self)) for accurate signature.

Parameters:
  • code_file (TypeAliasForwardRef('~manim.typing.StrPath') | None)

  • code_string (str | None)

  • language (str | None)

  • formatter_style (str | type[Style])

  • tab_width (int)

  • add_line_numbers (bool)

  • line_numbers_from (int)

  • background (Literal['rectangle', 'window'])

  • background_config (dict[str, Any] | None)

  • paragraph_config (dict[str, Any] | None)

classmethod get_pygments_style(name)[source]

Return the Pygments style registered under name.

Parameters:

name (str) – The name of the Pygments style to retrieve.

Returns:

The corresponding Pygments style class.

Return type:

type[Style]

classmethod get_styles_list()[source]

Get the list of all available formatter styles.

Return type:

list[str]