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='#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
ofCode
is aVGroup
consisting of three objects:The background,
listing.background_mobject
. This is either aRectangle
(if the listing has been initialized withbackground="rectangle"
, the default option) or aVGroup
resembling a window (ifbackground="window"
has been passed).The line numbers,
listing.line_numbers
(aParagraph
object).The highlighted code itself,
listing.code
(aParagraph
object).
Warning
Using a
Transform
on text with leading whitespace (and in this particular case: code) can look weird. Consider usingremove_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 ¶
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)
- 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().
- line_numbers#
The line numbers for the code listing. Empty, if
insert_line_no=False
has been specified.- Type
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.