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

Knowing if ipynb is run from Jupyter or nbconvert #2587

Open
quertenmont opened this issue Jun 20, 2017 · 6 comments
Open

Knowing if ipynb is run from Jupyter or nbconvert #2587

quertenmont opened this issue Jun 20, 2017 · 6 comments

Comments

@quertenmont
Copy link

Hello,

Is there a way to detect from a notebook cell whether the code is being run from a jupyter notebook or from a nbconvert command.
I would like to decide how to render some graphics based on this flag

  • if in jupyter I would use interactive (HTML/JS/D3) type of graphics
  • if from nbconvert I would use matplotlib that could be exported to PDF.

Thanks in advance for your help,
Loic

@takluyver
Copy link
Member

Sorry, by design there isn't - we expect nbconvert to produce similar results whether it's using saved outputs in the notebook, or generating new outputs by re-executing it. If you're using nbconvert as part of a pipeline you control, you could set an environment variable and check it in your code to switch behaviour.

@quertenmont
Copy link
Author

Can't I get some info by executing some javascript to get the page URL for instance ?

@takluyver
Copy link
Member

Not very reliably, because of the asynchronous nature of Javascript.

@JarnoRFB
Copy link

JarnoRFB commented Jun 3, 2019

you could set an environment variable and check it in your code to switch behaviour

would it be bad for nbconvert to set this environment variable itself? That would be convenient.

@cunnane
Copy link

cunnane commented Jun 28, 2023

Agree with @JarnoRFB here - whilst I appreciate the design philosophy of having nbconvert indistinguishable from jupyter, in practice, when ipywidgets.interact or other any other interactive features get involved, my experience is that nbconvert is not always able to render these correctly. It would be much easier to allow the user to step in rather than the nbconvert team to handle all possible futures cases.

@dlyongemallo
Copy link

Is the above still true, or is there a better way to detect when nbconvert is running than to set an environment variable? We have a notebook which has interactive javascript, which we want to output as a static image when producing a PDF from sphinx.

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

No branches or pull requests

5 participants