Widgets
code_video.PartialCode
Renders source code files or strings in part, delineated by line numbers
__init__(self, path=None, code=None, extension='py', start_line=1, end_line=None, **kwargs)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Optional[str] |
The source code file path. Either this or |
None |
code |
Optional[List[str]] |
A list of code lines as strings. Either this or |
None |
extension |
str |
The code extension, required if using |
'py' |
Source code in code_video/code_walkthrough.py
def __init__(
self,
path: Optional[str] = None,
code: Optional[List[str]] = None,
extension: str = "py",
start_line: int = 1,
end_line: Optional[int] = None,
**kwargs,
):
"""
Args:
path: The source code file path. Either this or `code` must be set
code: A list of code lines as strings. Either this or `path` must be set
extension: The code extension, required if using `code`
"""
if not code and not path:
raise ValueError("Must define file_name or code")
if code and not extension:
raise ValueError("Must define the extension with the code")
if not extension:
extension = path.split(".")[-1]
if path:
with open(path, "r") as f:
code = f.readlines()
if end_line:
code = code[start_line - 1 : end_line]
else:
code = code[start_line - 1 :]
with NamedTemporaryFile(suffix=f".{extension}") as f:
f.writelines([line.encode() for line in code])
f.flush()
super().__init__(f.name, line_no_from=start_line, **kwargs)
code_video.TextBox
A text with a box around it
__init__(self, text, text_attrs=None, **kwargs)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str |
The text to display |
required |
Source code in code_video/widgets.py
def __init__(self, text: str, text_attrs=None, **kwargs):
"""
Args:
text: The text to display
"""
if text_attrs is None:
text_attrs = {"size": 0.5, "font": DEFAULT_FONT}
super().__init__(text, text_attrs=text_attrs, **kwargs)
self._box(
text=text,
border_builder=lambda title: Rectangle(
height=_get_text_height(title) + self.border_padding, width=title.get_width() + self.border_padding
),
)
code_video.NoteBox
Text with a note box around it
__init__(self, text, text_attrs=None, **kwargs)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str |
The text to display |
required |
Source code in code_video/widgets.py
def __init__(self, text: str, text_attrs=None, **kwargs):
"""
Args:
text: The text to display
"""
if text_attrs is None:
text_attrs = {"size": 0.5, "font": DEFAULT_FONT}
super().__init__(text, text_attrs=text_attrs, **kwargs)
def build_border(title: Text):
ear_size = title.get_width() * 0.05
w = title.get_width() + 0.3 * 2
h = title.get_height() + 0.3
return Polygon((0, h, 0), (w - ear_size, h, 0), (w, h - ear_size, 0), (w, 0, 0), (0, 0, 0), (0, h, 0))
self._box(text=text, border_builder=build_border)
code_video.Connection
An arrow connection between two objects
__init__(self, source, target, label=None, font='sans-serif', **kwargs)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
Mobject |
The source object |
required |
target |
Mobject |
The target object |
required |
label |
Optional[str] |
The optional label text to put over the arrow |
None |
Source code in code_video/widgets.py
def __init__(self, source: Mobject, target: Mobject, label: Optional[str] = None, font=DEFAULT_FONT, **kwargs):
"""
Args:
source: The source object
target: The target object
label: The optional label text to put over the arrow
"""
super().__init__(font=font, **kwargs)
self.font = font
label_direction = UP
label_buff = 0
arrow: Optional[Arrow] = None
if source.get_x(RIGHT) <= target.get_x(LEFT):
arrow = Arrow(start=source.get_edge_center(RIGHT), end=target.get_edge_center(LEFT), buff=0)
label_direction = UP
elif source.get_x(LEFT) >= target.get_x(RIGHT):
arrow = Arrow(start=source.get_edge_center(LEFT), end=target.get_edge_center(RIGHT), buff=0)
label_direction = UP
elif source.get_y(DOWN) >= target.get_y(UP):
arrow = Arrow(start=source.get_edge_center(DOWN), end=target.get_edge_center(UP), buff=0)
label_direction = RIGHT
label_buff = VERTICAL_ARROW_LABEL_BUFF
elif source.get_y(UP) <= target.get_y(DOWN):
arrow = Arrow(start=source.get_edge_center(UP), end=target.get_edge_center(DOWN), buff=0)
label_direction = RIGHT
label_buff = VERTICAL_ARROW_LABEL_BUFF
if not arrow:
raise ValueError("Unable to connect")
self.add(arrow)
if label:
text = Text(label, font=self.font, size=0.5, slant=ITALIC)
text.next_to(arrow, direction=label_direction, buff=label_buff)
self.add(text)