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.
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.
line_numbers
[source]¶The line numbers for the code listing. Empty, if
insert_line_no=False
has been specified.
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):
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
Function put text color to those strings that don’t have one according to background_color of displayed code. |
|
Function to validate file. |
|
Function to background_color, generate code_json and tab_spaces from html_string. |
|
Function to generate code. |
|
Function to generate html string with code highlighted and stores in variable html_string. |
|
Function to generate line_numbers. |
Attributes
|
Used to animate the application of a method. |
|
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.
line_str (str
) – Takes a html element’s string to put color to it according to background_color of displayed code.
The generated html element’s string with having color attributes.
str
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.
The generated code according to parameters.