Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

New feature: allow passing a code string to Code (#948)

Benjamin Hacklbf6c56e73a7a
diff --git a/manim/mobject/svg/code_mobject.py b/manim/mobject/svg/code_mobject.py
index 06c55e7e5..5fd2d29ca 100644
--- a/manim/mobject/svg/code_mobject.py
+++ b/manim/mobject/svg/code_mobject.py
@@ -52,6 +52,9 @@ class Code(VGroup):
----------
file_name : :class:`str`
Name of the code file to display.
+ code : :class:`str`
+ If ``file_name`` is not specified, a code string can be
+ passed directly.
tab_width : :class:`int`, optional
Number of space characters corresponding to a tab character. Defaults to 3.
line_spacing : :class:`float`, optional
@@ -115,13 +118,26 @@ class Code(VGroup):
language="cpp",
)
- Remove unwanted invisible characters::
+ We can also render code passed as a string (but note that
+ the language has to be specified in this case):
+
+ .. manim:: CodeFromString
+ :save_last_frame:
- self.play(Transform(remove_invisible_chars(listing.code.chars[0:2]),
- remove_invisible_chars(listing.code.chars[3][0:3])))
+ class CodeFromString(Scene):
+ def construct(self):
+ code = '''from manim import Scene, Square
- remove_invisible_chars(listing.code)
- remove_invisible_chars(listing)
+ 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)
"""
@@ -136,6 +152,7 @@ class Code(VGroup):
def __init__(
self,
file_name=None,
+ code=None,
tab_width=3,
line_spacing=0.3,
scale_factor=0.5,
@@ -177,8 +194,18 @@ class Code(VGroup):
self.language = language
self.generate_html_file = generate_html_file
- self.file_name = file_name or self.file_name
- self.ensure_valid_file()
+ self.file_path = None
+ self.file_name = file_name
+ if self.file_name:
+ self.ensure_valid_file()
+ with open(self.file_path, "r") as f:
+ self.code_string = f.read()
+ elif code:
+ self.code_string = code
+ else:
+ raise ValueError(
+ "Neither a code file nor a code string have been specified."
+ )
self.style = self.style.lower()
self.gen_html_string()
strati = self.html_string.find("background:")
@@ -328,11 +355,8 @@ class Code(VGroup):
def gen_html_string(self):
"""Function to generate html string with code highlighted and stores in variable html_string."""
- file = open(self.file_path, "r")
- code_str = file.read()
- file.close()
self.html_string = hilite_me(
- code_str,
+ self.code_string,
self.language,
self.style,
self.insert_line_no,
@@ -533,9 +557,13 @@ def hilite_me(
cssstyles=defstyles + divstyles,
prestyles="margin: 0",
)
- if language is None:
+ if language is None and file_path:
lexer = guess_lexer_for_filename(file_path, code)
html = highlight(code, lexer, formatter)
+ elif language is None:
+ raise ValueError(
+ "The code language has to be specified when rendering a code string"
+ )
else:
html = highlight(code, get_lexer_by_name(language, **{}), formatter)
if insert_line_no: