4 KiB
Exercise 16: Custom cell tags
With the extended functionality of live code, additional cell tags have been developed that can be added to Python code cells in any .ipynb file. These tags extend the existing Jupyter Book tags and allow you to control how cells behave when live execution is enabled.
::::::{topic} Exercise objective Can you apply custom cell tags to control the execution and visibility of code cells? ::::::
Available custom tags
disable-execution-celldisables the ability to execute the cell when live code is activated. This is useful if your notebook includes interactivity for only part of the cells.disable-execution-pagedisables the ability to execute the entire page. This can be helpful if Python code is used to build a page but is not meant to be run by readers.auto-execute-pageautomatically starts live coding when the page is opened, useful if you are using widgets.thebe-initruns this cell automatically as soon as live coding starts.thebe-remove-input-initis similar tothebe-initbut hides the input from students while still executing it. (The regularremove-inputtag will not work here, since it deletes the entire cell.)
Tags are case-sensitive. Use lowercase and match them exactly as shown above.
Creating a notebook
- In your repository, create a new notebook file in your book directory, e.g.
book/live_code_tags.ipynb. - Add a new code cell with something simple inside, for example:
print("Hello from live code!")
-
When you are ready, commit your changes to the repository by clicking on the green
Commit changesbutton. -
Add a commit message.
-
To see your changes, go to {octicon}
playActions- The most recent workflow runoverview.md / the commit message of the commit you just made- Wait for it to finish - In the summary, click on the link of your book shown in the tableBranches deployedand underPrimary book at root(getting bored of waiting? There'll be exercising on doing this locally which prevents you from waiting). -
Do you see your change? If you don't see it click
CTRL+F5/Control+F5to refresh the page.
disable-execution-cell
- Now let's go back to your notebook. Open the cell metadata (in JupyterLab or Jupyter Notebook:
View → Cell Toolbar → Tags). - Add the tag
disable-execution-cellto your code cell, and commit and rebuild your book. - Visit the page — notice that this particular cell can no longer be run when live code is activated.
disable-execution-page
- Add another code cell to your file, for example:
print("Goodbye from live code!")
- Add the tag
disable-execution-pageto any of the live cell codes in your file, and commit and rebuild your book. - Visit the page — none of the cells should be executable now.
Remove this tag again before continuing, otherwise the next tags won’t have any effect.
auto-execute-page
- Add the tag
auto-execute-pageto any cell and rebuild your book. - Visit the page — live code should automatically start running without clicking the “launch” button.
thebe-init
- Add the tag
thebe-initto any cell and rebuild your book. - Visit the page — as soon as live code starts, this cell will run by itself.
thebe-remove-input-init
- Replace the previous tag with
thebe-remove-input-init, and rebuild your book. - Visit the page — you should only see the output of the cell, not the code, but the cell is still executed automatically when live code starts.
:class: note
Before moving on, make sure you understand the following:
- How do you add tags to a cell in an `.ipynb` file?
- Which tag disables execution for a single cell? For the whole page?
- Which tag makes a page auto-start live code when opened?
- What is the difference between `thebe-init` and `thebe-remove-input-init`?