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

Provide way to determine if running in VS Code Interactive Notebook #7380

Closed
brettcannon opened this issue Sep 1, 2021 · 4 comments
Closed
Assignees
Labels
notebook-workflow Issues that interrupt expected or desirable behavior

Comments

@brettcannon
Copy link
Member

Discussed in microsoft/vscode-python#16034

Originally posted by MarcSkovMadsen April 24, 2021
I am a user of and contributor to HoloViz Panel. Panel is a framework for interactive exploratory data analysis and for building powerful analytics apps (see https://awesome-panel.org).

Panel builds on the Bokeh ecosystem and Panel provides full interactivity in the VS Code Interactive Notebook environment if I set pn.extension(comms='vscode') at the top of my code. See the Panel - VS Code docs.

My pain is that I and the users I develop notebooks for work in many environments Notebook, Labs, VS Code, PyCharm, Colab and deploy to many servers Bokeh/ Panel, Voila, Flask, Django etc. And the line of code pn.extension(comms='vscode') should not be added in any environment but the VS Code Interactive Notebook environment.

So I need a way to programmatically determine if the code is running in the VS Code Interactive Notebook environment.

Solution

Please tell me how to programmatically from python determine if I am running in VS Code Interactive Notebook or develop something that can tell me.

Thanks

Additional Context

I tried printing sys.modules to see if something vs code python extension specific is loaded. But without luck.

image

I have also tried Google and Github to find an answer without luck.

Not working with 3.0

Panel is currently not working in VS Code with jupyter >= 3.0 c.f. bokeh/jupyter_bokeh#131. Some js package needs to be deployed to npm.

Panel and VS Code is a super powerful combination

The below is something you can develop with Panel. It can also run interactively inside Notebooks and VS Code. And it even lets you start additional servers via .show() method on Panel objects so that can open and update windows outside of VS Code from the Interactive Notebook. I.e. circumventing the problem of VS Code not really supporting working efficiently with multiple windows".

deckgl_binder.mp4

It also has a powerful --autoreload function for users for hot reloading of .py and .ipynb files. Which makes it super powerful for working with VS Code and Python.

autoreload (1)

@brettcannon brettcannon transferred this issue from microsoft/vscode-python Sep 1, 2021
@rchiodo rchiodo removed their assignment Sep 1, 2021
@rchiodo
Copy link
Contributor

rchiodo commented Sep 1, 2021

I answered this in the discussion but the only way to do this now is to examine the environment of the launched kernel. It will contain VS code environment variables (for local kernels).

We could potentially add another environment variable to a kernel that says something like - VS_CODE_STARTED_KERNEL

@MarcSkovMadsen
Copy link

MarcSkovMadsen commented Sep 2, 2021

Panel 0.12 used the information from this discussion to support VS Code out of the NOx. Thanks for the help 👍

@greazer greazer added notebook-workflow Issues that interrupt expected or desirable behavior and removed needs-triage labels Sep 2, 2021
@DonJayamanne
Copy link
Contributor

DonJayamanne commented Sep 2, 2021

@MarcSkovMadsen can you try to use the environment variable VSCODE_PID .
That should work (its an existing environment variable).

@DonJayamanne
Copy link
Contributor

Closing this issue for now, please feel free to re-open or create a new issue if this doesn't work

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
notebook-workflow Issues that interrupt expected or desirable behavior
Projects
None yet
Development

No branches or pull requests

5 participants