-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notebook (.ipynb) file support for yaml generator
- Loading branch information
Juha Kiili
committed
Jun 29, 2021
1 parent
12fa08e
commit 8c5bee3
Showing
2 changed files
with
98 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
import json | ||
import os | ||
import shlex | ||
from typing import Union, List | ||
|
||
|
||
# TODO: This file is a copy-pasta from https://github.com/valohai/jupyhai | ||
# TODO: DRY between libs | ||
|
||
|
||
def parse_ipynb(content_or_str: Union[str, dict]) -> dict: | ||
""" | ||
"Smartly" parse content that contains a notebook. | ||
* If a string, it's first JSON deserialized. | ||
* If it's a "wrapped" dict (i.e. contains "type" == "notebook" and "content"), unwraps the content | ||
* Asserts the content smells like a notebook ("nbformat") | ||
:param content: See above. | ||
:return: Notebook data. | ||
""" | ||
if isinstance(content_or_str, str): | ||
content = json.loads(content_or_str) | ||
else: | ||
content = content_or_str | ||
if not isinstance(content, dict): | ||
raise ValueError('Ipynb not a dict') | ||
assert isinstance(content, dict) | ||
if content.get('type') == 'notebook': | ||
content = content['content'] | ||
|
||
nbformat = content.get('nbformat') | ||
if not isinstance(nbformat, int): | ||
raise ValueError('Nbformat value %s invalid' % nbformat) | ||
return content | ||
|
||
|
||
def get_notebook_source_code(contents: dict) -> str: | ||
source = [cell['source'] for cell in contents['cells'] if cell['cell_type'] == 'code'] | ||
|
||
# Some notebook versions store it as list of rows already. Some as single string. | ||
source = [row if isinstance(row, list) else row.split('\n') for row in source] | ||
|
||
# Even when it was a list, the linefeeds are still there. | ||
source = [row.rstrip() for sublist in source for row in sublist] | ||
|
||
# Strip magics like "!pip install tensorflow" | ||
source = [row for row in source if not row.startswith("!")] | ||
|
||
return '\n'.join(source) | ||
|
||
|
||
def get_notebook_command(notebook_relative_path) -> List[str]: | ||
notebook_dir, notebook_name = os.path.split(notebook_relative_path) | ||
papermill_command = " ".join([ | ||
"papermill -k python3 -f /valohai/config/parameters.yaml", | ||
shlex.quote("/valohai/repository/{}".format(notebook_relative_path.replace(os.sep, "/"))), | ||
shlex.quote("/valohai/outputs/{}".format(notebook_name.replace(os.sep, "/"))), | ||
]) | ||
return [ | ||
"pip install -r requirements.txt", | ||
papermill_command | ||
] |
This file contains 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