โ๏ธ Introduction to Jupyter Notebooks in JupyterLab
Now that youโve seen the REPL in iPython, from now on in this class you will code in Jupyter Notebooks. Jupyter is an incredibly awesome and user-friendly integrated development environment (IDE). An IDE provides a place for data scientists to see and work with a bunch of different aspects of their work in a nice, organized interface.
Always remember: Python is the programming language. Jupyter Lab is an IDE.
1. Meet JupyterLab
JupyterLab provides a nice user interface for data science, development, reporting, and collaboration (all of which youโll learn about throughout the MEDS program) in one place.
Features of JupyterLab as an IDE
Interactive Computing: JupyterLab is designed primarily for interactive computing and data analysis. It supports live code execution, data visualization, and interactive widgets, which are key features of modern IDEs.
Multi-language Support: While originally developed for Python, JupyterLab supports many other programming languages through the use of kernels, making it versatile for various programming tasks.
Rich Text Editing: It provides a rich text editor for creating and editing Jupyter Notebooks, which can contain both code and narrative text (Markdown), allowing for documentation and code to coexist.
Code Execution: JupyterLab allows you to execute code cells and see the output immediately, making it suitable for testing and iterating on code quickly.
File Management: It includes a file manager for browsing and managing project files, similar to the file explorers found in traditional IDEs.
Extensions and Customization: JupyterLab supports numerous extensions that can enhance its capabilities, such as version control integration, terminal access, and enhanced visualizations.
Integrated Tools: It has an integrated terminal, variable inspector, and other tools that are typically part of an IDE, providing a comprehensive environment for development.
Differences from Traditional IDEs
Focus on Notebooks: Unlike many traditional IDEs that focus on scripting and full-scale software development, JupyterLab emphasizes the use of notebooks for exploratory data analysis and research.
Non-linear Workflow: JupyterLab allows for a non-linear workflow, where users can execute cells out of order and iteratively modify and test code.
JupyterLab Interface
Primary panes include the Main Work Area pane, Sidebar, and Menu Bar.
As you work, Jupyer Lab will add additional tabs/panes that contain figures and data inspectors, or even other file types. You can rearrange these panes to organize your workspace however you like.
You can check out the JupyterLab User Guide for tons of information and helpful tips!
JupyterLab is a powerful interactive development environment (IDE) that allows you to work with Jupyter Notebooks, text editors, terminals, and other components in a single, integrated environment. Itโs widely used in data science, scientific computing, and education.
Getting Started with Jupyter Notebooks in JupyterLab
Creating a New Notebook
Open JupyterLab: Once JupyterLab is running, youโll see the JupyterLab interface with the file browser on the left.
Create a New Notebook:
Click on the + button in the file browser to open a new Launcher tab.
Under the โNotebookโ section, click โPython 3โ to create a new Python notebook.
Rename the Notebook:
Click on the notebook title (usually โUntitledโ) at the top of the notebook interface.
Enter a new name for your notebook and click โRenameโ.
Understanding the Notebook Interface
The Jupyter Notebook interface is divided into cells. There are two main types of cells:
Code Cells: For writing and executing Python code.
Markdown Cells: For writing formatted text using Markdown syntax.
Writing and Running Code
Letโs start by writing some simple Python code in a code cell.
Add a Code Cell:
Click inside the cell and start typing your Python code.
# Simple Python codeprint("Hello, Jupyter!")
Run the Code Cell:
Click the โRunโ button in the toolbar or press Shift + Enter to execute the code.
The output will be displayed directly below the cell.
Writing Markdown
Markdown cells allow you to write formatted text. You can use Markdown to create headings, lists, links, and more.
Add a Markdown Cell:
Click on the โ+โ button in the toolbar to add a new cell.
Change the cell type to โMarkdownโ from the dropdown menu in the toolbar.
Write Markdown Text:
# My First Markdown CellThis is a simple example of a Markdown cell in JupyterLab.## Features of Markdown- **Bold Text**: Use `**text**` for **bold**.- **Italic Text**: Use `*text*` for *italic*.- **Lists**: Create bullet points using `-` or `*`.- **Links**: [JupyterLab Documentation](https://jupyterlab.readthedocs.io/)
Render the Markdown:
Click the โRunโ button or press Shift + Enter to render the Markdown text.
Combining Code and Markdown
Jupyter Notebooks are powerful because they allow you to combine code and markdown in a single document. This is useful for creating interactive tutorials, reports, and data analyses.
Rendering Images
Jupyter Notebooks can render images directly in the output cells, which is particularly useful for data visualization.
Example: Displaying an Image
Code
from IPython.display import Image, display# Display an imageimg_path ='https://jupyterlab.readthedocs.io/en/stable/_images/interface-jupyterlab.png'display(Image(url=img_path, width=700))
Interactive Features
Jupyter Notebooks support interactive features, such as widgets, which enhance the interactivity of your notebooks.
Example: Using Interactive Widgets
Widgets allow users to interact with your code and visualize results dynamically.
Code
import ipywidgets as widgets# Create a simple slider widgetslider = widgets.IntSlider(value=50, min=0, max=100, step=1, description='Value:')display(slider)
Saving and Exporting Notebooks
Save the Notebook:
Click the save icon in the toolbar or press Ctrl + S (Cmd + S on macOS) to save your work.
Export the Notebook:
JupyterLab allows you to export notebooks to various formats, such as PDF or HTML. Go to File > Export Notebook As and choose your desired format.
Tips for Using JupyterLab
Keyboard Shortcuts: Familiarize yourself with keyboard shortcuts to speed up your workflow. You can view shortcuts by clicking Help > Keyboard Shortcuts. You can also refer to our class Jupyter Keyboard Shortcuts Cheatsheet
Using the File Browser: Drag and drop files into the file browser to upload them to your workspace.
Using the Variable Inspector: The variable inspector shows variable names, types, values/shapes, and counts (for collections). Open the Variable Inspector using Menu: View > Activate Command Palette, then type โvariable inspector.โ Or use the keyboard shortcut: Ctrl + Shift + I (Windows/Linux) or Cmd + Shift + I (Mac)
Conclusion
JupyterLab is a versatile tool that makes it easy to combine code, text, and visualizations in a single document. By mastering the basic functionality of Jupyter Notebooks, you can create powerful and interactive documents that enhance your data analysis and scientific computing tasks.
Feel free to experiment with the code and markdown examples provided in this guide to deepen your understanding of JupyterLab. Happy coding!
Resources
We will get to know Jupyter Notebooks very well during the rest of this course, but here are even more resources you can use to learn and revisit:
Jupyter Notebook Gallery
There are many, many examples of textbooks, academic articles, journalism, analyses, and reports written in Jupyter Notebooks. Here is a link to a curated gallery containing many such examples. Itโs worth exploring some of these just to get a sense of the diversity of applications and opportunities available using python and jupyter in data science!
Description: The official documentation for Jupyter Notebooks provides a comprehensive guide to installing, using, and customizing notebooks. It includes tutorials, tips, and examples to help you get started.
Description: This page offers an interactive โTry Jupyterโ experience, allowing you to run Jupyter Notebooks in the browser without installing anything locally. It is a great way to explore the basics of Jupyter in a hands-on manner.
Description: This YouTube series provides an in-depth introduction to Jupyter Notebooks. Corey Schafer covers installation, basic usage, and advanced features, making it easy to follow along and practice on your own.
Description: This tutorial on DataCampโs community blog offers a step-by-step guide to using Jupyter Notebooks for data science. It covers the basics and explores more advanced topics such as widgets and extensions.
Description: This Real Python article introduces Jupyter Notebooks, covering installation, basic usage, and tips for using notebooks effectively. It is an excellent resource for Python developers who are new to Jupyter.
Description: Google Colab is a free platform that lets you run Jupyter Notebooks in the cloud. You can find many tutorials and example notebooks on their site. For example, here is a link to a notebook theyโve created that includes many pandas snippets.