jupytext
jupytext copied to clipboard
Print output also to command line
Would it be possible to also print the output of the script to the command line? I looked through the docs, but didn't find a solution yet.
I.e. when I run
jupytext script.py --to=ipynb --execute
that i also see the outputs as if I would have executed
python script.py
?
Thanks!
Hey @HelgeGehring , yes that is possible but you should use the --log-output option of papermill.
For instance if I run
jupytext script.py --set-kernel - --to ipynb -o - | papermill output.ipynb --log-output
on this script:
# %%
from time import sleep
# %%
print("A")
# %%
sleep(2)
# %%
print("B")
# %%
sleep(2)
# %%
print("C")
then this is what gets displayed on the command line:
Input Notebook: -
Output Notebook: output.ipynb
/home/marc/micromamba/envs/jupytext-dev/lib/python3.12/site-packages/papermill/iorw.py:149: UserWarning: the file is not specified with any extension : -
warnings.warn(f"the file is not specified with any extension : {os.path.basename(path)}")
Executing notebook with kernel: python3
Executing Cell 1---------------------------------------
Ending Cell 1------------------------------------------
Executing Cell 2---------------------------------------
A
Ending Cell 2------------------------------------------
Executing Cell 3---------------------------------------
Ending Cell 3------------------------------------------
Executing Cell 4---------------------------------------
B
Ending Cell 4------------------------------------------
Executing Cell 5---------------------------------------
Ending Cell 5------------------------------------------
Executing Cell 6---------------------------------------
C
Ending Cell 6------------------------------------------
sounds amazing, I'll give it a try, thanks a lot!!
would you have an idea how to also show the content of each cell? papermill doesn't have a "--log-input"
One time I converted from the nbconvert HTML version back to .txt (with pandoc iirc) to paste a notebook with inputs and outputs to a mailing list; like IPython %doctest_mode.
- https://github.com/mwouts/jupytext/issues/220#issuecomment-1486007291
Hi @mwouts, would you consider making --log-output (or similar) a built-in feature of jupytext so users don't have to resort to the non-obvious papermill trick you provided above?
Perhaps also make this the default behavior of jupytext --execute, since that is probably what most users expect?
Thanks!
Well, since there are a few of you asking sure yes I can consider that. The problem is that I'd rather stay with nbconvert for executing the notebook and at first sight it's not obvious that we can add a postprocess cell hook? They have a preprocess hook however: https://nbconvert.readthedocs.io/en/latest/api/preprocessors.html#executing-notebooks .
Wonderful you're open to this! It would simplify things a lot for us over here.
Too bad this is currently blocked on nbconvert. I searched their tracker and didn't see anything about this, so I just opened https://github.com/jupyter/nbconvert/issues/2212. Hope that helps, and please chime in there with any additional input. Thanks again for your work on jupytext!