Giovanni
Giovanni

Reputation: 293

How can I programmatically check that I am running code in a notebook in julia?

I would need to programmatically check that I am running code in a jupyter notebook from Julia. One way would be using

isdefined(Main, :IJulia)

However this does not work for notebooks within vscode since they are run from outside IJulia is there a check that would work in this case as well?

Upvotes: 1

Views: 434

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

What about @__FILE__ this yields REPL[_] in Julia REPL, In[_] in Jupyter and "/path/to/file.jl#==#hashocde" in Pluto so the test could be:

match(r"^In\[[0-9]*\]$", @__FILE__) != nothing

and in VSCode: enter image description here

so you can check if the file ends with ".ipynb" if you want to find VSCode. Moreover: isdefined(Main, :VSCodeServer) yields true if you run from VSCode.

Upvotes: 1

Related Questions