Markdown interspersing long code?

I’m totally new to jupyter. I’ve just started to use CoCalc. My purpose is to show a ~100-line code to collaborators and let them run it on their screens. Because the code will be too long for a single screen, I want to structure my Jupyter notebook(?) like
Explanation1 (markdown or some text)
Code1
Explanation2
Code2
. . . .

Here, Code1, Code2, . . . form a single continuous program.

As a test, I wrote Code1 in a cell and Code2 in the next cell. But, when I ran it, Code2 didn’t see the definitions in Code1.

So, I suppose that the code snippets are treated as independent complete programs. How should I proceed? I’m using Julia on CoCalc.

Something doesn’t sound correct. You can go here and click the launch binder badge below the text to launch a session with Julia as the kernel backing the notebook.

If you add a cell below, after you run the second code cell there in the demo notebook, with the following content, you should see the output as 2.6930971830599457 when you run the new cell:

s = s +2
s

This new cell uses the namespace of the notebook which defined s as 0.6930971830599458 in the previous cell.

If you aren’t seeing this in CoCalc, something is seriously different.
Is the code in a cell causing the kernel to fail and restart before you try your second cell?

1 Like

Thank you so much for the information! and I’m sorry for the confusion.

Now that I know the code is supposed to be continuous across multiple cells, I tried again. And it worked!

Before posting my first question above, I didn’t know that Jupyter’s “run” function runs one cell at a time. After moving the last part of the code to a new cell, probably I pressed the run button on the new cell. That’s why I got the “undefined variable” error.

So, I thought I needed to call a special function or “use” a special module when running a Julia program on Jupyter.

(Now I need a button that runs all the cells from top to bottom.)

Thanks again for clarification. That was of great help to me.

You should have a Run All cells menu option. (It’s under the ‘Run’ menu in JupyterLab.)
Plus a really nice Restart Kernel and Run All that is great for making sure you didn’t accidentally delete something and the current state relies on something no longer present. You’ll see this referred to as the fact notebooks can have a ‘hidden state’. Good to get in a habit or rerunning everything when you can to make sure you haven’t mistakenly introduced such a state during development.
That restart kernel and run all option in JupyterLab is both under the ‘Run’ menu and ‘Kernel’ menu.
I’m pretty sure you can bind those to any keyboard shortcut you’d prefer so that you can use a combination of keys to trigger those. In JupyterLab, go to ‘Settings’ > ‘Advanced Settings Editor’ > ‘Keyboard Shortcuts’. You edit in the panel on the right under ‘User Preferences’. This discussion might help in making such shortcuts.

You tagged your post with ‘JupyterLab’ and so that is why I mentioned where to find those options in relation to JupyterLab.The classic notebook interface that opens with the link in my last post should have similar options under ‘Cell’ and ‘Kernel’. Editing shortcuts is under ‘Help’. I know presently the classic interface is less customizable than JuplyterLab. (Eventually, the future classic looking interface will actually have JupyterLab under the hood, and so down the road customizations will be more consistent where there are parallels.)

2 Likes

Thanks! I’ve found the button to restart the kernel and run all. Initially I missed it because it looks like the fastforward button of a CD player.

I actually don’t know whether my post fits better in JupyterLab or in Notebook. CoCalc says it’s incorporated all that JupyterLab offers, which has made me think it’s more similar to JupyterLab than Notebook.

1 Like