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

A way to check if code is running in NBInclude #21

Closed
knuesel opened this issue Sep 14, 2021 · 5 comments · Fixed by #24
Closed

A way to check if code is running in NBInclude #21

knuesel opened this issue Sep 14, 2021 · 5 comments · Fixed by #24

Comments

@knuesel
Copy link

knuesel commented Sep 14, 2021

It would be nice to have a function available in notebooks to check if code is being run by NBInclude. I currently do something like this:

in_nbinclude() = contains(@__FILE__, r".:In\[.*\]$")

# then in later cells:

...
if !in_nbinclude()
    display(...)  # This won't display anything when run with nbinclude
end
...

This is related to #20. However using cell exclusions like in #20 requires setting regex in every @nbinclude call.

@stevengj
Copy link
Collaborator

stevengj commented Sep 15, 2021

I guess we could add an in_nbinclude() = _in_nbinclude[] function in the NBInclude modue, where we define the global const _in_nbinclude = Ref(false) and then set _in_nbinclude[] = true in the nbinclude function by changing ret = my_include_string(m, s, string(path, ":In[", cellnum, "]"), prev, softscope) to

ret = try
    _in_nbinclude[] = true
    my_include_string(m, s, string(path, ":In[", cellnum, "]"), prev, softscope)
finally
    _in_nbinclude[] = false
end

Do you want to take a crack at a PR?

@knuesel
Copy link
Author

knuesel commented Sep 17, 2021

That looks good, thanks! I'm happy to make a PR (no time right now but in a few days).

@stevengj
Copy link
Collaborator

Thinking about it a bit more, it should probably use task-local storage rather than a single global variable.

@stevengj
Copy link
Collaborator

stevengj commented Sep 17, 2021

In particular, define:

const _in_nbinclude = :in_nbinclude_0db19996_df87_5ea3_a455_e3a50d440464 # unique symbol based on our UUID4
in_nbinclude() = get(Base.task_local_storage(), _in_nbinclude, false)::Bool

and then do

ret = Base.task_local_storage(_in_nbinclude, true) do
    my_include_string(m, s, string(path, ":In[", cellnum, "]"), prev, softscope)
end

@knuesel
Copy link
Author

knuesel commented Oct 1, 2021

Thanks! (sorry for the silence, it's a busy time...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants