-
Notifications
You must be signed in to change notification settings - Fork 508
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
feat: plaintext support #1499
base: main
Are you sure you want to change the base?
feat: plaintext support #1499
Changes from all commits
04fb8f6
3208e39
9b18767
c90b425
df2005b
5ef7892
c7457b0
a235323
fe26497
6438eef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from .maker import * | ||
from .export import * | ||
from .imports import * | ||
from .process import plaintext_file_formats | ||
|
||
from fastcore.script import * | ||
from fastcore.utils import * | ||
|
@@ -120,15 +121,18 @@ def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', skip_file_r | |
"Find all files in a directory matching an extension given a config key." | ||
path = Path(path or get_config()[key]) | ||
recursive=get_config().recursive | ||
res = globtastic(path, file_glob=file_glob, skip_folder_re=skip_folder_re, | ||
skip_file_re=skip_file_re, recursive=recursive, **kwargs) | ||
if type(file_glob) != list: file_glob = [file_glob] | ||
res = [] | ||
for _file_glob in file_glob: | ||
res += globtastic(path, file_glob=_file_glob, skip_folder_re=skip_folder_re, | ||
skip_file_re=skip_file_re, recursive=recursive, **kwargs) | ||
return res.map(Path) if as_path else res | ||
|
||
# %% ../nbs/api/05_doclinks.ipynb | ||
def nbglob_cli( | ||
path:str=None, # Path to notebooks | ||
symlinks:bool=False, # Follow symlinks? | ||
file_glob:str='*.ipynb', # Only include files matching glob | ||
file_glob:Union[str, List[str]]=['*.ipynb'] + [f"*.{ext}" for ext in plaintext_file_formats], # Only include files matching glob | ||
file_re:str=None, # Only include files matching regex | ||
folder_re:str=None, # Only enter folders matching regex | ||
skip_file_glob:str=None, # Skip files matching glob | ||
|
@@ -142,7 +146,7 @@ def nbglob_cli( | |
@call_parse | ||
@delegates(nbglob_cli) | ||
def nbdev_export( | ||
path:str=None, # Path or filename | ||
path:str=None, # Path or filename, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a mistake? |
||
procs:Param("tokens naming the export processors to use.", nargs="*", choices=optional_procs())="black_format", | ||
**kwargs): | ||
"Export notebooks in `path` to Python modules" | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,14 @@ | |||||||
|
||||||||
from collections import defaultdict | ||||||||
|
||||||||
try: | ||||||||
import nbformat | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imports can be on one line |
||||||||
import jupytext | ||||||||
import tempfile | ||||||||
plaintext_supported = True | ||||||||
except ImportError: | ||||||||
plaintext_supported = False | ||||||||
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
# %% ../nbs/api/03_process.ipynb | ||||||||
# from https://github.com/quarto-dev/quarto-cli/blob/main/src/resources/jupyter/notebook.py | ||||||||
langs = defaultdict( | ||||||||
|
@@ -88,17 +96,43 @@ def _mk_procs(procs, nb): return L(procs).map(instantiate, nb=nb) | |||||||
# %% ../nbs/api/03_process.ipynb | ||||||||
def _is_direc(f): return getattr(f, '__name__', '-')[-1]=='_' | ||||||||
|
||||||||
# %% ../nbs/api/03_process.ipynb | ||||||||
plaintext_file_formats = { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be on one line. |
||||||||
"pct.py": "py:percent", | ||||||||
"lgt.py": "py:light", | ||||||||
"spx.py": "py:sphinx", | ||||||||
"myst.md": "md:myst", | ||||||||
"pandoc.md": "md:pandoc", | ||||||||
} | ||||||||
|
||||||||
# %% ../nbs/api/03_process.ipynb | ||||||||
class NBProcessor: | ||||||||
"Process cells and nbdev comments in a notebook" | ||||||||
def __init__(self, path=None, procs=None, nb=None, debug=False, rm_directives=True, process=False): | ||||||||
self.nb = read_nb(path) if nb is None else nb | ||||||||
def __init__(self, path=None, procs=None, nb=None, debug=False, rm_directives=True, process=False, fmt=None): | ||||||||
self._handle_nb(path, nb, fmt) | ||||||||
self.lang = nb_lang(self.nb) | ||||||||
for cell in self.nb.cells: cell.directives_ = extract_directives(cell, remove=rm_directives, lang=self.lang) | ||||||||
self.procs = _mk_procs(procs, nb=self.nb) | ||||||||
self.debug,self.rm_directives = debug,rm_directives | ||||||||
if process: self.process() | ||||||||
|
||||||||
def _handle_nb(self, path, nb, fmt): | ||||||||
path = str(path) | ||||||||
if any(path.endswith(ext) for ext in plaintext_file_formats) or fmt is not None: | ||||||||
fmt = plaintext_file_formats[".".join(path.rsplit('.', 2)[-2:])] if fmt is None else fmt | ||||||||
if fmt in plaintext_file_formats.values(): | ||||||||
if not plaintext_supported: | ||||||||
raise ValueError(f"File {path} has a supported extension, but plaintext conversion is not supported. Please install jupytext and nbformat to use this feature.") | ||||||||
nb_converted = jupytext.read(path, fmt=fmt) | ||||||||
with tempfile.NamedTemporaryFile(delete=True, suffix=".ipynb") as temp_file: | ||||||||
nbformat.write(nb_converted, temp_file.name) | ||||||||
self.nb = read_nb(temp_file.name) if nb is None else nb | ||||||||
return | ||||||||
if fmt is None or fmt == "ipynb": | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each part here can be on one line. |
||||||||
self.nb = read_nb(path) if nb is None else nb | ||||||||
else: | ||||||||
raise ValueError(f"Invalid format: {fmt}") | ||||||||
|
||||||||
def _process_cell(self, proc, cell): | ||||||||
if not hasattr(cell,'source'): return | ||||||||
if cell.cell_type=='code' and cell.directives_: | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be a list comprehension.