How to write a notebook for Times Square¶
If you know how to create a Jupyter notebook, publishing a notebook to Times Square will be familiar. This page outlines the basic steps for adding and authoring a notebook for Times Square, and links to further documentation.
1. Setting up on the Science Platform Notebook Aspect¶
Although its not strictly necessary, the best place to create and edit notebooks for Times Square is from the Rubin Science Platform’s Notebook Aspect. If the notebook runs from your interactive session, it will run on Times Square (provided it doesn’t use user-specific software or data, see Notebooks can’t reference files or code installed in a specific user’s home directory).
First start a JupyterLab session on the Notebook Aspect. Then from a JupyterLab terminal, clone a GitHub repository that’s already installed on Times Square:
git clone https://github.com/lsst-sqre/times-square-usdf
cd times-square-usdf
Make sure you’ve set up GitHub access in your JupyterLab environment (see Configuring Git for GitHub). You’ll “publish” your notebook to Times Square by pushing it to GitHub.
2. Creating a new notebook¶
Create a new notebook and save it into the repository you cloned above:
From the JupyterLab Launcher, create a new notebook with the “LSST” kernel.
Save the notebook into the repository you cloned above. Select
and set the notebook’s path and name.
Make sure the path is within the repository. You can also save the notebook into a subdirectory of the repository. The directory hierarchy is reflected in the Times Square interface, so it’s a useful tool for organizing notebooks.
3. Scaffold the notebook cells¶
Times Square uses the first code cell in the notebook for setting parameters. It’s useful to mark this cell with a comment so that you don’t forget about its special role.
4. Develop the notebook’s content¶
Now you can develop the notebook’s content as you would normally. Remember that in Times Square notebooks, the user will focus on the cell outputs, and by default the code cells are hidden.
While writing the notebook, keep in mind the requirements that Times Square places on notebooks. For example, your notebook can’t access user-specific data or software and can’t access modules or data within the GitHub repository.
5. Parameterize the notebook¶
Parameters allow the user to set values that a notebook uses in its computations. In the example notebook, a user should be able to set the date when the moon illumination is computed.
The general process for parameterizing a notebook is:
Add a variable to the parameters cell and assign a default value. The value of this variable is always a Python string even if you intend to treat it as a specific Python type (see Parameter types).
Edit the notebook to use that parameter variable (or variables). In your code you might need to convert the parameter value to a Python type. See Parameter types for examples.
6. Create a metadata sidecar file¶
The metadata sidecar is a YAML file that contains information about the notebook and its parameterization.
It always has the same path and name as the notebook, but with a .yaml
extension.
This file always goes alongside its notebook, hence the term “sidecar.”
In the JupyterLab Launcher, create a new text file. Select
to save the file with the same path and name as the notebook, but with a.yaml
extension.Alternatively you can edit the file with a terminal app like
vim
oremacs
:vim example.yaml
Populate the file with information about the title and parameters:
title: Example description: Moon illumination on a given night. authors: - name: Jonathan Sick slack: jonathansick parameters: date: type: string description: Night (YYYY-MM-DD) default: "2024-02-01"
See Sidecar metadata YAML file reference for more information about the sidecar file’s schema.
Save the file.
7. Commit and push the notebook to GitHub¶
Open a JupyterLab terminal and navigate to the repository:
Create a branch, if you haven’t already:
git switch -c tickets/EXAMPLE
Stage and commit both the notebook and metadata sidecar files:
git add example.ipynb example.yaml git commit -m "Add example notebook"
Push the branch to GitHub:
git push -u origin tickets/EXAMPLE
8. Create a pull request¶
Create a Pull Request on GitHub that proposes to merge your branch into main
(or the default branch, in general).
See the GitHub Pull Request documentation for more information.
9. Review the notebook on Times Square¶
While your pull request is open, Times Square will run the notebooks and check the formatting of the sidecar metadata files.
You can view the notebook on Times Square by click on the “Notebook execution” check towards the bottom of the pull request page and then click on the notebook’s name from the check run status.
10. Merge the pull request¶
While your pull request is open, you can continue to make changes to the notebook and its metadata sidecar file. Whenever you push an update, Times Square will re-check and re-run the notebook. Therefore you can keep trying your notebook on Times Square until you’re satisfied with the results.
When you’re ready to merge the pull request, click the “Merge pull request” button on the pull request page. At this point, the notebook will be available from the Times Square homepage.