manim.mobject.svg.code_mobject.Code

class Code(file_name=None, code=None, tab_width=3, line_spacing=0.3, scale_factor=0.5, font='Monospac821 BT', stroke_width=0, margin=0.3, indentation_chars='    ', background='rectangle', background_stroke_width=1, background_stroke_color='#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, **kwargs)[source]

Bases: manim.mobject.types.vectorized_mobject.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.

Parameters
  • file_name (str) – Name of the code file to display.

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

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

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

  • scale_factor (class:float, optional) – A number which scales displayed code. Defaults to 0.5.

  • font (str, optional) – The name of the text font to be used. Defaults to "Monospac821 BT".

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

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

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

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

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

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

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

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

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

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

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

  • language (Optional[str], optional) – 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, optional) – Defines whether to generate highlighted html code to the folder assets/codes/generated_html_files. Defaults to False.

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

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)

Methods

correct_non_span

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

ensure_valid_file

Function to validate file.

gen_code_json

Function to background_color, generate code_json and tab_spaces from html_string.

gen_colored_lines

Function to generate code.

gen_html_string

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

gen_line_numbers

Function to generate line_numbers.

Attributes

animate

Used to animate the application of a method.

animation_overrides

depth

The depth of the mobject.

height

The height of the mobject.

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