Code#

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

class Code(file_name=None, code=None, tab_width=3, line_spacing=0.3, font_size=24, font='Monospace', stroke_width=0, margin=0.3, indentation_chars='    ', background='rectangle', background_stroke_width=1, background_stroke_color=ManimColor('#FFFFFF'), corner_radius=0.2, insert_line_no=True, line_no_from=1, line_no_buff=0.4, style='vim', language=None, generate_html_file=False, warn_missing_font=True, **kwargs)[source]#

Bases: VGroup

A highlighted source code listing.

An object listing of Code is a VGroup consisting of three objects:

  • The background, listing.background_mobject. This is either a Rectangle (if the listing has been initialized with background="rectangle", the default option) or a VGroup resembling a window (if background="window" has been passed).

  • The line numbers, listing.line_numbers (a Paragraph object).

  • The highlighted code itself, listing.code (a Paragraph object).

Warning

Using a Transform on text with leading whitespace (and in this particular case: code) can look weird. Consider using remove_invisible_chars() to resolve this issue.

Examples

Normal usage:

listing = Code(
    "helloworldcpp.cpp",
    tab_width=4,
    background_stroke_width=1,
    background_stroke_color=WHITE,
    insert_line_no=True,
    style=Code.styles_list[15],
    background="window",
    language="cpp",
)

We can also render code passed as a string (but note that the language has to be specified in this case):

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=code, tab_width=4, background="window",
                            language="Python", font="Monospace")
        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=code, tab_width=4, background="window",
                            language="Python", font="Monospace")
        self.add(rendered_code)

Parameters:
  • file_name (str | os.PathLike | None) – Name of the code file to display.

  • code (str | None) – If file_name is not specified, a code string can be passed directly.

  • tab_width (int) – Number of space characters corresponding to a tab character. Defaults to 3.

  • line_spacing (float) – Amount of space between lines in relation to font size. Defaults to 0.3, which means 30% of font size.

  • font_size (float) – A number which scales displayed code. Defaults to 24.

  • font (str) – The name of the text font to be used. Defaults to "Monospace". This is either a system font or one loaded with text.register_font(). Note that font family names may be different across operating systems.

  • stroke_width (float) – Stroke width for text. 0 is recommended, and the default.

  • margin (float) – Inner margin of text from the background. Defaults to 0.3.

  • indentation_chars (str) – “Indentation chars” refers to the spaces/tabs at the beginning of a given code line. Defaults to "    " (spaces).

  • background (str) – Defines the background’s type. Currently supports only "rectangle" (default) and "window".

  • background_stroke_width (float) – Defines the stroke width of the background. Defaults to 1.

  • background_stroke_color (str) – Defines the stroke color for the background. Defaults to WHITE.

  • corner_radius (float) – Defines the corner radius for the background. Defaults to 0.2.

  • insert_line_no (bool) – Defines whether line numbers should be inserted in displayed code. Defaults to True.

  • line_no_from (int) – Defines the first line’s number in the line count. Defaults to 1.

  • line_no_buff (float) – Defines the spacing between line numbers and displayed code. Defaults to 0.4.

  • style (str) – Defines the style type of displayed code. You can see possible names of styles in with styles_list. Defaults to "vim".

  • language (str | None) – Specifies the programming language the given code was written in. If None (the default), the language will be automatically detected. For the list of possible options, visit https://pygments.org/docs/lexers/ and look for ‘aliases or short names’.

  • generate_html_file (bool) – Defines whether to generate highlighted html code to the folder assets/codes/generated_html_files. Defaults to False.

  • warn_missing_font (bool) – If True (default), Manim will issue a warning if the font does not exist in the (case-sensitive) list of fonts returned from manimpango.list_fonts().

background_mobject#

The background of the code listing.

Type:

VGroup

line_numbers#

The line numbers for the code listing. Empty, if insert_line_no=False has been specified.

Type:

Paragraph

code#

The highlighted code.

Type:

Paragraph

Methods

Attributes

animate

Used to animate the application of any method of self.

animation_overrides

color

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

styles_list

width

The width of the mobject.

_correct_non_span(line_str)[source]#

Function put text color to those strings that don’t have one according to background_color of displayed code.

Parameters:

line_str (str) – Takes a html element’s string to put color to it according to background_color of displayed code.

Returns:

The generated html element’s string with having color attributes.

Return type:

str

_ensure_valid_file()[source]#

Function to validate file.

_gen_code_json()[source]#

Function to background_color, generate code_json and tab_spaces from html_string. background_color is just background color of displayed code. code_json is 2d array with rows as line numbers and columns as a array with length 2 having text and text’s color value. tab_spaces is 2d array with rows as line numbers and columns as corresponding number of indentation_chars in front of that line in code.

_gen_colored_lines()[source]#

Function to generate code.

Returns:

The generated code according to parameters.

Return type:

Paragraph

_gen_html_string()[source]#

Function to generate html string with code highlighted and stores in variable html_string.

_gen_line_numbers()[source]#

Function to generate line_numbers.

Returns:

The generated line_numbers according to parameters.

Return type:

Paragraph

_original__init__(file_name=None, code=None, tab_width=3, line_spacing=0.3, font_size=24, font='Monospace', stroke_width=0, margin=0.3, indentation_chars='    ', background='rectangle', background_stroke_width=1, background_stroke_color=ManimColor('#FFFFFF'), corner_radius=0.2, insert_line_no=True, line_no_from=1, line_no_buff=0.4, style='vim', language=None, generate_html_file=False, warn_missing_font=True, **kwargs)#

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

Parameters:
  • file_name (str | os.PathLike | None) –

  • code (str | None) –

  • tab_width (int) –

  • line_spacing (float) –

  • font_size (float) –

  • font (str) –

  • stroke_width (float) –

  • margin (float) –

  • indentation_chars (str) –

  • background (str) –

  • background_stroke_width (float) –

  • background_stroke_color (str) –

  • corner_radius (float) –

  • insert_line_no (bool) –

  • line_no_from (int) –

  • line_no_buff (float) –

  • style (str) –

  • language (str | None) –

  • generate_html_file (bool) –

  • warn_missing_font (bool) –