How to check if Julia is running in a vscode notebook?

I’ve noticed a few issues using packages like ProgressMeter and Bio3Dview in the VS code notebook. ProgressMeter keeps appending the progress bar to new lines. It shows repeated output in VScode · Issue #231 · timholy/ProgressMeter.jl · GitHub

BioView even says that it doesn’t think it is running in a notebook context

using BioStructures
using Bio3DView
struc = retrievepdb("3LVA")
viewstruc(struc['A'])

output:

┌ Info: File exists: 3LVA
└ @ BioStructures /Users/ben/.julia/packages/BioStructures/1QcQI/src/pdb.jl:236
"Cannot run the command as you do not appear to be in an IJulia notebook, Blink is not loaded and html is not set to true"

These examples in jupterlab work as intended.

It seems like this rendering breaks down because the checks for if julia is running in a notebook returns false:

# returns false in vscode notebooks
isdefined(Main, :IJulia) && Main.IJulia.inited 

What would be a check for if julia is running in the VS code notebook context?

What I use is

isdefined(Main, :VSCodeServer)

That half works, It tells if julia is being run in VScode, but not if it is in the notebook or if it is being run in a script with output sent to the REPL.

For instance using isdefined(Main, :VSCodeServer) with Bio3DView, it is able to output the correct output cell in the notebook but then doesn’t work anymore in a script context, just outputting the HTML directly into the REPL rather than opening the output pane

isinteractive() seems to be what I am looking for.

is_vscode_notebook() = isdefined(Main, :VSCodeServer) && !isinteractive()

seems to only return true in the vscode notebook context, and not when sending code to the repl from vscode, or when running the script from the command line.

1 Like