Skip to content

Commit

Permalink
add anshook to run a function on returned values in cells (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC authored and stevengj committed May 16, 2017
1 parent 86c0b4f commit 247be63
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ all processes.)
* In IJulia, cells beginning with `;` or `?` are interpreted as shell commands or help requests, respectively. Such cells are ignored by `nbinclude`.
* `counters` and `regex` keywords can be used to include a subset of notebook cells to those for which `counter ∈ counters` and the cell text matches `regex`. For example, `nbinclude("notebook.ipynb"; counters=1:10, regex=r"#\s*EXECUTE")`
would include cells 1 to 10 from `notebook.ipynb` that contain comments like `# EXECUTE`.
* A keyword `anshook` can be used to run a passed function on the return value of all the cells.
* No Python or Jupyter dependency.

To install it, simply do `Pkg.add("NBInclude")` as usual for Julia packages.
Expand Down
8 changes: 6 additions & 2 deletions src/NBInclude.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function my_include_string(s::AbstractString, path::AbstractString, prev)
end

"""
nbinclude(path::AbstractString; renumber::Bool=false, counters=1:typemax(Int), regex::Regex=r"")
nbinclude(path::AbstractString; renumber::Bool=false, counters=1:typemax(Int), regex::Regex=r"", anshook = identity)
Include the IJulia Jupyter notebook at `path` and execute the code
cells (in the order that they appear in the file), returning the
Expand All @@ -65,10 +65,13 @@ are executed. E.g.
would include cells 1 to 10 from "notebook.ipynb" that contain comments like
`# exec` or `# ExecuteMe` in the cell text.
`anshook` can be used to execute a function on all the values returned in the cells.
"""
function nbinclude(path::AbstractString; renumber::Bool=false,
counters = 1:typemax(Int),
regex::Regex = r"")
regex::Regex = r"",
anshook = identity)
# act like include(path), in that path is relative to current file:
prev = Base.source_path(nothing)
path = (prev == nothing) ? abspath(path) : joinpath(dirname(prev),path)
Expand Down Expand Up @@ -107,6 +110,7 @@ function nbinclude(path::AbstractString; renumber::Bool=false,
string(cell["execution_count"])
counter in counters && ismatch(regex, s) || continue
ret = my_include_string(s, string(path, ":In[", cellnum, "]"), prev)
anshook(ret)
end
end
return ret
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ x=[]; nbinclude("test2.ipynb"; regex=r"#.*executeme")

x=[]; nbinclude("test2.ipynb"; counters = [1, 4, 5], regex=r"#.*executeme")
@test x == [4]

z = 0; nbinclude("test2.ipynb"; anshook = x -> (global z += 1))
@test z == 6

0 comments on commit 247be63

Please sign in to comment.