-
Notifications
You must be signed in to change notification settings - Fork 10
feature: notebook as python file #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Red-Giuliano
wants to merge
17
commits into
main
Choose a base branch
from
pyfile-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6508cb2
updated docs with higher res screenshots and to fix typos/errors in code
7582120
minor docs updates and saving/reading as python file
e105280
made slight change to handle multine cell definitions in parser
72bdad8
excluding notebook.py from reload in dev cli
a9b0567
notebook saves as a bunch of python functions
490e7f7
python file updates
2da72c5
minimal viable implementation
b1ea9f2
added return statements and requirements for functions
41d97b2
adding yarn lock from main
539e1a3
minor mod to remove extra spaces
3620a92
had to yarn-install in order to run, minor mode to support nested fun…
ad23243
saving as python file exclusively
df4eb52
restored yarn lock to working state
94d76fd
fixed test_e2e wrong var name
12bf066
fixing nitpicks
f36c1a0
fixed failing ipynb conversion
abeef31
fixed failing tests and new cells not adding
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,36 @@ | ||
from zt_backend.models.state.notebook_state import NotebookState | ||
from zt_backend.models import notebook | ||
import rtoml | ||
import subprocess | ||
from pathlib import Path | ||
import os | ||
|
||
notebook_state = NotebookState() | ||
import importlib.util | ||
|
||
IPYNB_PATH = Path("zt_backend/tests/test_file.ipynb").resolve() | ||
OUTPUT_PATH = Path("zt_backend/tests/notebook.ztnb").resolve() | ||
NOTEBOOK_PATH = Path("zt_backend/tests/test_notebook.ztnb").resolve() | ||
OUTPUT_PATH = Path("zt_backend/tests/notebook.py").resolve() | ||
NOTEBOOK_PATH = Path("zt_backend/tests/test_notebook.py").resolve() | ||
|
||
def dynamic_import(module_path): | ||
spec = importlib.util.spec_from_file_location("notebook_module", module_path) | ||
module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
return module | ||
|
||
def test_ipynb_to_ztnb(): | ||
|
||
# Step 1: Convert the IPYNB file to a Python file | ||
convert = subprocess.Popen( | ||
["zero-true", "jupyter-convert", IPYNB_PATH, OUTPUT_PATH] | ||
) | ||
convert.wait() | ||
|
||
with open(NOTEBOOK_PATH, "r", encoding="utf-8") as file: | ||
expected_data = rtoml.loads(file.read().replace("\\", "\\\\")) | ||
|
||
expected_notebook_data = { | ||
"notebookId": "test_id", | ||
"notebookName": expected_data.get("notebookName", "Zero True"), | ||
"userId": "", | ||
"cells": { | ||
f"{index}": notebook.CodeCell(id=f"{index}", **cell_data, output="") | ||
for index, (cell_id, cell_data) in enumerate(expected_data["cells"].items()) | ||
}, | ||
} | ||
expected_notebook = notebook.Notebook(**expected_notebook_data) | ||
|
||
with open(OUTPUT_PATH, "r", encoding="utf-8") as file: | ||
output_data = rtoml.loads(file.read().replace("\\", "\\\\")) | ||
|
||
output_notebook_data = { | ||
"notebookId": "test_id", | ||
"notebookName": output_data.get("notebookName", "Zero True"), | ||
"userId": "", | ||
"cells": { | ||
f"{index}": notebook.CodeCell(id=f"{index}", **cell_data, output="") | ||
for index, (cell_id, cell_data) in enumerate(output_data["cells"].items()) | ||
}, | ||
} | ||
output_notebook = notebook.Notebook(**output_notebook_data) | ||
|
||
assert expected_notebook == output_notebook | ||
|
||
os.remove(OUTPUT_PATH) | ||
# Step 2: Import the expected notebook from test_notebook.py | ||
from test_notebook import notebook as expected_notebook | ||
output_module = dynamic_import(OUTPUT_PATH) | ||
output_notebook = output_module.notebook | ||
# Step 3: Import the generated notebook from notebook.py | ||
expected_notebook.notebookId='0' | ||
output_notebook.notebookId='0' | ||
# Step 4: Assert that the generated notebook matches the expected notebook | ||
assert output_notebook == expected_notebook | ||
|
||
# Step 5: Clean up the generated file | ||
os.remove(OUTPUT_PATH) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import zero_true as zt | ||
|
||
def cell_0(): | ||
print('Hello, world!') | ||
|
||
def cell_1(): | ||
zt.markdown("""# This is a markdown cell""") | ||
|
||
notebook = zt.notebook( | ||
id='ca156d73-1c20-48c6-afbb-ae177c6bafa5', | ||
name='Zero True', | ||
cells=[ | ||
zt.cell(cell_0, type='code'), | ||
zt.cell(cell_1, type='markdown'), | ||
] | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.