JuliaInterop / NBInclude.jl

import code from IJulia Jupyter notebooks into Julia programs
Other
118 stars 17 forks source link
julia jupyter jupyter-notebook

NBInclude

CI

NBInclude is a package for the Julia language which allows you to include and execute IJulia (Julia-language Jupyter) notebook files just as you would include an ordinary Julia file.

The goal of this package is to make notebook files just as easy to incorporate into Julia programs as ordinary Julia (.jl) files, giving you the advantages of a notebook (integrated code, formatted text, equations, graphics, and other results) while retaining the modularity and re-usability of .jl files.

Basic usage

Analogous to include("myfile.jl") in Julia to execute myfile.jl, you can do

using NBInclude
@nbinclude("myfile.ipynb")

to execute all of the code cells in the IJulia notebook myfile.ipynb. Similar to include, the value of the last evaluated expression in the last evaluated code cell is returned.

We also export an in_nbinclude() function, which returns true only when it is executed in code run via @nbinclude. Using this, you can selectively run code in a notebook only interactively or only via @nbinclude.

There is also a function

nbexport("myfile.jl", "myfile.ipynb")

that can be used to convert an IJulia notebook file to an ordinary Julia file, with Markdown text in the notebook converted to formatted comments in the Julia file.

Detailed features

Key features of @nbinclude are:

Note: Scoping rules differ between interactive (IJulia, REPL) and non-interactive Julia code. Running a notebook as @nbinclude("foo.ipynb"; softscope=true) will load notebooks using "soft" global scoping similar to interactive (REPL) code in Julia 1.5+ or for IJulia with any Julia version. That flag's default value, false, will load notebooks with the "hard" scoping rule that Julia uses for non-interactive code (e.g. in include); see also the SoftGlobalScope package for more details.

Key features of nbexport are:

Contact

NBInclude was written by Steven G. Johnson and is free/open-source software under the MIT/Expat license. Please file bug reports and feature requests at the NBInclude github page.