Skip to content
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

Cells with cell magic output the contents as a really long string! #101

Open
vlulla opened this issue Aug 2, 2022 · 2 comments
Open

Cells with cell magic output the contents as a really long string! #101

vlulla opened this issue Aug 2, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@vlulla
Copy link

vlulla commented Aug 2, 2022

  • nbautoexport version: 0.5.0
  • Python version: 3.9.13
  • Operating System: Linux

Description

The output of cell with cell magic is a function call with a very long string! For instance look at this example output from the corresponding cell (cell number 15) from the notebook. The generated python script is hard to navigate/review!

If calling the run_cell_magic function is unavoidable, can at least the third argument be formatted to read somewhat like normal python source code (maybe using triple quote string)?


Here is a small example of the above behavior. For a notebook with two cells:

# Cell 1
import sys
print(sys.version)

and

# Cell 2
%%time
x = 1
print(x)

the script output is:

#!/usr/bin/env python
# coding: utf-8

import sys
print(sys.version)


get_ipython().run_cell_magic('time', '', 'x = 1\nprint(x)')

It would be tremendously helpful if the timed cell content appeared like printed python code.

@jayqi
Copy link
Member

jayqi commented Aug 2, 2022

I think this is standard nbconvert behavior, which reflects how IPython cell magics actually work under the hood. If you wanted to run the converted code with IPython and have it work (including doing whatever the cell magics are supposed to do), this would be the correct code.

Agreed though that this is not very good for humans reading the script files, which is the main use case we have for nbautoexport.

This thread seems like a good starting place for finding information to customize nbconvert to get us what we want. In particular, this recent comment seems like it's for recent versions of nbconvert.

@jayqi jayqi added the enhancement New feature or request label Aug 2, 2022
@vlulla
Copy link
Author

vlulla commented Aug 3, 2022

The recent comment indeed looks like a nice way to get this to work! Nice find @jayqi ! Given that nbautoexport depends on nbconvert maybe the install procedure for nbautoexport can create this small customization (a python function + jinja template + conf.json) as a part of default install or a custom install option? I think the bulk of what is needed to be done is in that comment but some of the edge cases will have to be included in tests (just to be sure).

The only problem with this is that running the notebook and the generated script won't be running the same code, as far as I understand it. This too will need to be checked/tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants