Skip to content

Commit 3fa0ecc

Browse files
committed
Jupyter notebook conversion supported
1 parent d94afdf commit 3fa0ecc

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/pyscript/_generator.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import jinja2
55

66
from ._node_parser import find_imports, FinderResult
7+
from ._node_parser import _convert_notebook
8+
9+
10+
class UnsupportedFileType(Exception):
11+
pass
12+
713

814
_env = jinja2.Environment(
915
loader=jinja2.PackageLoader("pyscript"), trim_blocks=True, lstrip_blocks=True
@@ -33,8 +39,28 @@ def file_to_html(
3339
Warnings will be returned when scanning for environment, if any.
3440
"""
3541
output_path = output_path or input_path.with_suffix(".html")
36-
import_results = find_imports(input_path)
42+
43+
fname, extension = input_path.name, input_path.suffix
44+
if extension == ".py":
45+
with open(input_path, "rt") as f:
46+
source = f.read()
47+
48+
elif extension == ".ipynb":
49+
try:
50+
import nbconvert
51+
except ImportError as e: # pragma no cover
52+
raise ImportError(
53+
"Please install nbconvert to serve Jupyter Notebooks."
54+
) from e
55+
source = _convert_notebook(input_path)
56+
57+
else:
58+
raise UnsupportedFileType(
59+
"{} is neither a script (.py) nor a notebook (.ipynb)".format(fname)
60+
)
61+
62+
import_results = find_imports(source, input_path)
3763
with input_path.open("r") as fp:
38-
string_to_html(fp.read(), title, output_path, pyenv=import_results)
64+
string_to_html(source, title, output_path, pyenv=import_results)
3965
if import_results.has_warnings:
4066
return import_results

src/pyscript/_node_parser.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
from ._supported_packages import PACKAGE_RENAMES, STANDARD_LIBRARY, PYODIDE_PACKAGES
1313

1414

15-
class UnsupportedFileType(Exception):
16-
pass
17-
18-
1915
class NamespaceInfo:
2016
def __init__(self, source_fpath: Path) -> None:
2117
# expanding base_folder to absolute as pkgutils.FileFinder will do so - easier for later purging
@@ -164,14 +160,16 @@ def _convert_notebook(source_fpath: Path) -> str:
164160
return source
165161

166162

167-
def find_imports(source_fpath: Path,) -> FinderResult:
163+
def find_imports(source: str, source_fpath: Path,) -> FinderResult:
168164
"""
169165
Parse the input source, and returns its dependencies, as organised in
170166
the sets of external _packages, and local modules, respectively.
171167
Any modules or package with the same name found in the local
172168
173169
Parameters
174170
----------
171+
source : str
172+
Python source code to parse
175173
source_fpath : Path
176174
Path to the input Python module to parse
177175
@@ -182,24 +180,5 @@ def find_imports(source_fpath: Path,) -> FinderResult:
182180
This instance provides reference to packages and paths to
183181
include in the py-env, as well as any unsuppoted import.
184182
"""
185-
fname, extension = source_fpath.name, source_fpath.suffix
186-
if extension == ".py":
187-
with open(source_fpath, "rt") as f:
188-
source = f.read()
189-
190-
elif extension == ".ipynb":
191-
try:
192-
import nbconvert
193-
except ImportError as e: # pragma no cover
194-
raise ImportError(
195-
"Please install nbconvert to serve Jupyter Notebooks."
196-
) from e
197-
198-
source = _convert_notebook(source_fpath)
199-
200-
else:
201-
raise UnsupportedFileType(
202-
"{} is neither a script (.py) nor a notebook (.ipynb)".format(fname)
203-
)
204183

205184
return _find_modules(source, source_fpath)

0 commit comments

Comments
 (0)