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='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
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
) – Name of the code file to display.code (
str
) – Iffile_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.font_size (class:float, optional) – A number which scales displayed code. Defaults to 24.
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 toTrue
.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 withstyles_list
. Defaults to"vim"
.language (Optional[
str
], optional) – Specifies the programming language the given code was written in. IfNone
(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#
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.
sheen_factor
stroke_color
styles_list
width
The width of the mobject.