Introduction
According to the Jupyter website, "JupyterLab is the latest web-based interactive development environment for notebooks, code, and data. Its flexible interface allows users to configure and arrange workflows in data science, scientific computing, computational journalism, and machine learning. A modular design invites extensions to expand and enrich functionality."
Jupyter notebooks are a common Python instruction tool. They are used extensively in teaching because of their simplicity and versatility. JupyterLab is an interactive development environment (IDE). This quick start shows how to use Jupyter notebooks in JupyterLab.
Within Ansys, employees have access to Ansys Lab, which is a remote server running JupyterLab. All common PyAnsys libraries and other useful libraries, such as Matplotlib and NumPy, are pre-installed on Ansys Lab. This quick start is as applicable to Ansys Lab users as JupyerLab users.
If you are reading this as part of an Ansys Codefest, you should jump to the "Codefest" section in the article!
Prerequisites
You should read the Jupyter/IPython Notebook Quick Start Guide and view the Jupyter content in the Ansys Introduction to Python Scripting video.
- You must have Python installed.
- You must have the `jupyter` Python library installed. (For more information, see Installing Packages in the Python Packing User Guide.)
- You must have JupyterLab installed. (Installing JupyterLab automatically installs the `jupyter` Python library.)
Start JupyterLab
Once prerequisites are met, you can start JupyterLab:
- Navigate to the directory that you want to work in.
- Run the command `jupyter lab` in the address bar as shown in the following figure. Alternatively, from the same directory, you can run the command from a command line, powershell, or terminal.
After you press Enter, a terminal opens and runs some code before opening a new tab in your default browser.
Note: The tab in your browser is your JupyterLab environment. As long as this tab is open, JupyterLab is running.
Codefests
If you are participating in an Ansys Codefest then you should start reading from this point! When you first login to your lab session you will see two tabs. You should select the "Launcher" tab and click the "Start Codefest". The necessary notebook will be created, and opened, for you.
You should read the rest of the article as normal from here.
Create or open a Jupyter notebook
A Jupyter notebook has an IPYNB extension. Here is how you create a notebook:
- To create a notebook, in your working directory under the Notebook heading, click Python 3 (ipykernel).
- To open an existing notebook, double-click any that appear in the left pane.
The following image shows a zoomed in view of the page that appears.
JupyterLab is straightforward to use. You can place your mouse cursor over buttons to see tooltips.
The kernel is the current Python environment that is running. If you create an infinite loop accidentally, you can kill the kernel to kill the infinite Python process.
This image highlights the main features.
Here are descriptions for each box highlighting a feature:
- Dashed gray box: Area containing your notebook content.
- Black box: Advanced options that you don't need to worry about for now.
- Purple box: Menu options. You can click Help to access the documentation.
- Orange box: File explorer, which starts in the directory where you started JupyterLab. You cannot use the file explorer to navigate to directories higher than the starting directory.
- Blue box: Buttons for managing notebooks and files. The buttons from left to right perform these actions:
- Add a new file or notebook.
- Add a new directory.
- Upload a file.
- Refresh the file explorer,
- Green box: Shows tabs for each open notebook. You can have multiple notebooks open at a time.
- Yellow box: Main notebook commands. The buttons from left to right perform these operations on the active notebook:
- Save the notebook.
- Add a new cell.
- Cut selected cells.
- Copy selected cells.
- Paste copied cells.
- Run selected cells.
- Stop the kernel.
- Restart the kernel.
- Restart the kernel and run all cells in order.
- Select a cell from a dropdown list.
Notes on working with Jupyter Notebooks
- You can perform standard file operations within the file explorer by right-clicking. If you are using Ansys Lab, you can right-click and select an option for downloading files.
- Every statement in Python returns something, although often this is `None`. For more information, see Null in Python: Understanding Python's NoneType Object.
- The last statement that appears in a cell prints the object returned when the cell was run.
- Normally, Jupyter remembers which cells you have run and in what order.
- The square brackets to the left of every cell indicates the order that cell was run as well as its status
- [2] means "this cell was run second after the 1st cell"
- [*] means "this cell is currently running"
- [ ] means "this cell has not been run yet"
- However, if your kernel crashes or you have to restart for whatever reason, it forgets and all cells go back to [ ].
- You will need to re-run all the necessary cells up to your current cell, in the correct order, to recreate your previous behavior.