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

Add documentation explaining scoping and work-arounds #227

Closed
dlfivefifty opened this issue Oct 25, 2023 · 3 comments
Closed

Add documentation explaining scoping and work-arounds #227

dlfivefifty opened this issue Oct 25, 2023 · 3 comments

Comments

@dlfivefifty
Copy link
Contributor

I have a block of code like the following:

ret = 0
for k = 1:10
    ret += k
end
ret

Literate won't let me make a notebook unless I add global ret inside the for-loop. Even though the final Jupyter notebook is happy without the global ret.

Since this is the very first notebook in an introductory course I don't really want to talk about scoping, especially when I only need it to compile the file.

@fredrikekre
Copy link
Owner

This errors when you execute the notebook with Literate you mean? This is because Jupyter uses https://github.com/JuliaLang/SoftGlobalScope.jl so I guess Literate has to do the same for notebooks.

@dlfivefifty
Copy link
Contributor Author

I don't understand: if I run the code in a Jupyter cell it works perfectly fine.

Is there a way to have Literate just create the notebook without running the cells? That is, so it will succeed even if the cells contain invalid code.

@fredrikekre
Copy link
Owner

I don't understand: if I run the code in a Jupyter cell it works perfectly fine.

Yes, because Jupyter (like the REPL) uses different scoping rules compared to regular Julia files. This is not the case when Literate runs the code.

Is there a way to have Literate just create the notebook without running the cells?

Yes, pass execute = false to Literate.notebook.

@dlfivefifty dlfivefifty changed the title Scoping behaviour different in Literate.notebook than actual Jupyter notebook Add documentation explaining scoping and work-arounds Oct 25, 2023
fredrikekre added a commit that referenced this issue Nov 8, 2023
This patch enables "soft" scoping rules (see e.g.
https://github.com/JuliaLang/SoftGlobalScope.jl) for code execution
(markdown and notebook output). This is enabled by default for Jupyter
notebook output (to mimic how the IJulia kernel works), and disabled
otherwise. Soft scope rules can be enabled/disabled with the `softscope
:: Bool` configuration variable. Fixes #227.
fredrikekre added a commit that referenced this issue Nov 8, 2023
This patch enables "soft" scoping rules (see e.g.
https://github.com/JuliaLang/SoftGlobalScope.jl) for code execution
(markdown and notebook output). This is enabled by default for Jupyter
notebook output (to mimic how the IJulia kernel works), and disabled
otherwise. Soft scope rules can be enabled/disabled with the `softscope
:: Bool` configuration variable. Fixes #227.
fredrikekre added a commit that referenced this issue Nov 8, 2023
This patch enables "soft" scoping rules (see e.g.
https://github.com/JuliaLang/SoftGlobalScope.jl) for code execution
(markdown and notebook output). This is enabled by default for Jupyter
notebook output (to mimic how the IJulia kernel works), and disabled
otherwise. Soft scope rules can be enabled/disabled with the `softscope
:: Bool` configuration variable. Fixes #227.
fredrikekre added a commit that referenced this issue Nov 8, 2023
This patch enables "soft" scoping rules (see e.g.
https://github.com/JuliaLang/SoftGlobalScope.jl) for code execution
(markdown and notebook output). This is enabled by default for Jupyter
notebook output (to mimic how the IJulia kernel works), and disabled
otherwise. Soft scope rules can be enabled/disabled with the `softscope
:: Bool` configuration variable. Fixes #227.
fredrikekre added a commit that referenced this issue Nov 8, 2023
This patch enables "soft" scoping rules (see e.g.
https://github.com/JuliaLang/SoftGlobalScope.jl) for code execution
(markdown and notebook output). This is enabled by default for Jupyter
notebook output (to mimic how the IJulia kernel works), and disabled
otherwise. Soft scope rules can be enabled/disabled with the `softscope
:: Bool` configuration variable. Fixes #227.
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

No branches or pull requests

2 participants