Jupyter
Jupyter
System Requirements :
1.Windows:
Using Anaconda:
1.Go to Anaconda Navigator
2.Install Jupyter Notebook
3.Load Packages
4.Launch Jupyter Notebook
2.Ubuntu:
Command:
jupyter lab
jupyter notebook
* In the code cell area Enter and run the following command:
Creating Notebook:
To create a new notebook, click the "New" button in the top-right corner.
From the drop-down menu, select "Python 3" to open a new notebook.
After Creating Notebook it will show Code cell areas. The type of code
depends on the type of notebook you created.
Code cells are where you write and execute your code. When you run the code in a code
cell, Jupyter sends the code to the kernel (the computational engine), executes it, and
displays the output directly below the cell.
The type of code you can execute depends on the kernel of the notebook. For instance, if
you created a notebook with the Python 3 kernel, you would be able to write Python code
in the code cells.
Example:
2. Markdown Cells
Markdown is a lightweight markup language that allows you to format text easily. Jupyter
Notebooks support Markdown, enabling you to add headings, lists, equations, tables, and
more. To switch a cell to Markdown, you can use the cell type menu or keyboard shortcuts.
Formatting in Markdown:
•Headers: You can add headers by prefixing a line with one or more # symbols.
Example:
Input:
Output:
•Lists: Lists are simple to create by using * or - for bullet points. For nested lists, just
indent the list items.
Example:
Input:
Output:
Output:
•Tables: You can create tables in Markdown by separating columns with | and rows
with line breaks.
Example:
Input:
Output:
Output:
3. Raw NBConvert Cells
Raw cells are used to insert content that will not be evaluated by Jupyter. These cells are
useful if you want to include text or code that should remain unchanged when the
notebook is converted (e.g., when exporting to HTML, PDF, or other formats).
Raw cells are typically used for:
• Storing text or code that needs to be passed to another tool (e.g., Python code that
should only be rendered after being converted by nbconvert).
• Including content that is not meant for execution in the notebook itself.
Example: You can write full Python code in a raw cell that won't be executed until it's
processed by an external converter like nbconvert.
The Kernel:
Every notebook runs on a kernel, which is responsible for executing the code inside
the cells. When you execute a cell, the kernel runs the code and returns the output to be
displayed directly in the notebook.
Example:
import math
print(math.sqrt(16)) # Output: 4.0
If you run the above cell in one part of the notebook, you can use math in any subsequent
cells without re-importing it.
Installing Extensions
jupyter nbextension install extension_name
Managing Extensions
You can also manage extensions via the Nbextensions tab in the Jupyter Notebook
dashboard (if installed), where you can enable/disable extensions through a graphical
interface.
Popular Extensions
• Table of Contents: Auto-generates a table of contents.
• Codefolding: Allows code sections to be collapsed.
• Variable Inspector: Displays variables currently defined in the notebook.
• Autopep8: Automatically formats Python code.
Tools in JupyterNotebook:
Common Tools:
Cell Tags: Allows categorization of cells (e.g., "important", "summary").
Table of Contents - Base Numbering: Controls the starting number for table of
contents.
Advanced Tools:
1. Cell Metadata
Cell metadata provides additional functionality for customizing individual cells in
the notebook:
• Tags: Add custom labels for categorizing or identifying cells (e.g.,
important, summary).
• Editable: Control whether a cell can be edited or not (e.g., true or false).
• Type: Define how a cell behaves in a slideshow (e.g., slide, subslide,
fragment, skip).
2. Notebook Metadata
Notebook metadata affects the overall structure and environment of the
notebook:
• Kernel Specification: Defines the kernel to be used (e.g., Python, R).
• Language Info: Specifies language-related information such as
version and syntax highlighting settings.
• Table of Contents: Customizes table of contents numbering and
organization.
1. Markdown Enhancements
• LaTeX Support: Incorporate mathematical formulas seamlessly within the text.
• Example: $$ E = mc^2 $$
• Example:
| Name | Age |
|------|-----|
| John | 28 |
• Example: <h1>Title</h1>
2. Magic Commands
Jupyter has built-in “magic” commands that provide shortcuts for common tasks.
These commands usually start with one or two % or %% characters.
• Line Magics (operates on a single line):
• %time: Measures the execution time of a single line of code.
• %timeit: Measures the average execution time across multiple runs.
• %run: Executes an external Python script.
• %lsmagic: Lists all available magic commands in the current session.
• Cell Magics (applies to an entire cell):
• %%time: Times the execution of all code within a cell.
• %%capture: Captures the output of a cell (e.g., suppresses output or
redirects it).
• %%writefile: Writes the content of a cell to an external file.
3. Interactive Widgets
• ipywidgets: Integrate interactive controls like sliders, buttons, and input fields.
• Installation: pip install ipywidgets
• Example: widgets.IntSlider(min=0, max=10)
5. Keyboard Shortcuts
• Command Mode (Press Esc):
• A: Insert a new cell above the current one.
• B: Insert a new cell below the current one.
• D, D: Delete the selected cell.
• M: Convert the current cell to Markdown.
• Y: Convert the current cell to Code.
• Shift + Enter: Execute the current cell and move to the next.
• Edit Mode (Press Enter):
• Ctrl + /: Toggle comment/uncomment for selected lines.
• Tab: Auto-complete code or show suggestions.
• Shift + Tab: Display function signature and documentation.
• Example:
nbdiff notebook1.ipynb notebook2.ipynb