jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Print output also to command line

Open HelgeGehring opened this issue 1 year ago • 7 comments

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!

HelgeGehring avatar Oct 25 '24 21:10 HelgeGehring

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------------------------------------------

mwouts avatar Nov 01 '24 23:11 mwouts

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"

HelgeGehring avatar Nov 01 '24 23:11 HelgeGehring

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.

westurner avatar Mar 13 '25 23:03 westurner

  • https://github.com/mwouts/jupytext/issues/220#issuecomment-1486007291

westurner avatar Mar 13 '25 23:03 westurner

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!

ctcjab avatar Mar 31 '25 17:03 ctcjab

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 .

mwouts avatar Apr 03 '25 23:04 mwouts

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!

ctcjab avatar Apr 04 '25 13:04 ctcjab