Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Improve jupyter output file naming (#1649)

Ryan McCauley76d275f63081
diff --git a/manim/utils/ipython_magic.py b/manim/utils/ipython_magic.py
index d2f255ff8..add4d5435 100644
--- a/manim/utils/ipython_magic.py
+++ b/manim/utils/ipython_magic.py
@@ -1,9 +1,9 @@
"""Utilities for using Manim with IPython (in particular: Jupyter notebooks)"""
-import hashlib
import mimetypes
import os
import shutil
+from datetime import datetime
from pathlib import Path
from manim import config, tempconfig
@@ -111,7 +111,7 @@ else:
tmpfile = (
Path(config["media_dir"])
/ "jupyter"
- / f"{_video_hash(local_path)}{local_path.suffix}"
+ / f"{_generate_file_name()}{local_path.suffix}"
)
if local_path in self.rendered_files:
@@ -137,12 +137,5 @@ else:
)
-def _video_hash(path):
- sha1 = hashlib.sha1()
- with open(path, "rb") as f:
- while True:
- data = f.read(65536)
- if not data:
- break
- sha1.update(data)
- return sha1.hexdigest()
+def _generate_file_name():
+ return config["scene_names"][0] + "@" + datetime.now().strftime("%Y-%m-%d@%H-%M-%S")
diff --git a/tests/test_ipython_magic.py b/tests/test_ipython_magic.py
new file mode 100644
index 000000000..6b483533e
--- /dev/null
+++ b/tests/test_ipython_magic.py
@@ -0,0 +1,29 @@
+import re
+
+from manim import tempconfig
+from manim.utils.ipython_magic import _generate_file_name
+
+
+def test_jupyter_file_naming():
+ """Check the format of file names for jupyter"""
+ scene_name = "SimpleScene"
+ expected_pattern = r"[0-9a-zA-Z_]+[@_-]\d\d\d\d-\d\d-\d\d[@_-]\d\d-\d\d-\d\d"
+ with tempconfig({"scene_names": [scene_name]}):
+ file_name = _generate_file_name()
+ match = re.match(expected_pattern, file_name)
+ assert scene_name in file_name, (
+ "Expected file to contain " + scene_name + " but got " + file_name
+ )
+ assert match, "file name does not match expected pattern " + expected_pattern
+
+
+def test_jupyter_file_output(tmp_path):
+ """Check the jupyter file naming is valid and can be created"""
+ scene_name = "SimpleScene"
+ with tempconfig({"scene_names": [scene_name]}):
+ file_name = _generate_file_name()
+ actual_path = tmp_path.with_name(file_name)
+ with open(actual_path, "w") as outfile:
+ outfile.write("")
+ assert actual_path.exists()
+ assert actual_path.is_file()