0% found this document useful (0 votes)
9 views

Jupyter

Jupyter Notebooks is an open-source web application for creating and sharing documents that include live code, equations, visualizations, and narrative text, widely used in various fields like data science and education. It requires Python 3.7 or later and offers functionalities such as code execution, Markdown support, and interactive widgets, along with options for managing kernels and extensions. Users can install Jupyter via Anaconda or pip, and it supports various tools for enhanced functionality, including magic commands, rich outputs, and version control integration.

Uploaded by

kameshs366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Jupyter

Jupyter Notebooks is an open-source web application for creating and sharing documents that include live code, equations, visualizations, and narrative text, widely used in various fields like data science and education. It requires Python 3.7 or later and offers functionalities such as code execution, Markdown support, and interactive widgets, along with options for managing kernels and extensions. Users can install Jupyter via Anaconda or pip, and it supports various tools for enhanced functionality, including magic commands, rich outputs, and version control integration.

Uploaded by

kameshs366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 13

JUPYTER

Jupyter Notebooks is an open-source web application that allows you to


create and share documents that contain live code, equations, visualizations, and
narrative text. It's widely used in data science, machine learning, scientific research,
education, and any field that involves computational analysis and interactive
computing.

System Requirements :

1. Python 3.7 or later


2. Windows 7 or later
3. RAM: 4GB (atleast), 8GB or more is recommended
4. Storage: 1GB or more

Python IDE vs Jupyter:


Installation:

1.Windows:
Using Anaconda:
1.Go to Anaconda Navigator
2.Install Jupyter Notebook
3.Load Packages
4.Launch Jupyter Notebook

Using pip Cpmmand:


command to install Jupyter: python -m pip install jupyter
command to launch Jupyter using: jupyter notebook

2.Ubuntu:

Command:

pip install jupyterlab

jupyter lab

pip install notebook

jupyter notebook

To install python packages on Jupyter Notebook:

* Create a new Notebook or open an existing one.

* In the code cell area Enter and run the following command:

“ !pip install <package_name> “


How to Use Jupyter Notebook:

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.

Cells in Jupyter Notebook:


In Jupyter Notebooks, cells are the fundamental building blocks where you
write and execute code or text. Each cell can serve a different purpose depending
on its type. The box with a green outline in the screenshot represents a cell.

There are three main types of cells in Jupyter Notebooks:


1. Code Cells
2. Markdown Cells
3. Raw NBConvert Cells
1. Code Cells

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:

•LaTeX Equations: Markdown supports LaTeX for mathematical equations. Enclose


inline equations with $, and block equations with $$.
Example:
Input:

Output:

•Tables: You can create tables in Markdown by separating columns with | and rows
with line breaks.
Example:

Input:

Output:

•Text Formatting: You can bold or italicize text by enclosing it in ** or *, respectively.


Example:
Input:

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.

Key Points about the Kernel:


• Shared State: The kernel maintains the state across the entire notebook. For
example, if you import a module in one cell, it will be available in all subsequent
cells in the notebook.
• Execution Order: Each time a cell is executed, Jupyter assigns an execution count
(e.g., In[1], In[2], etc.) to indicate the order in which cells were run.

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.

Kernel Management Options


Jupyter provides several options for managing the kernel, especially useful when you need
to reset the environment or clear outputs:
1. Restart: Resets the kernel by clearing all variables and imports.
2. Restart and Clear Output: Resets the kernel and clears the output below each
cell, useful when you want to re-run everything from scratch.
3. Restart and Run All: Resets the kernel and runs all cells in the notebook from top
to bottom.
4. Interrupt: Interrupts the kernel, which can be helpful if the code execution is taking
too long or is stuck.

Jupyter Notebook Extensions:


Jupyter extensions add new functionality to notebooks, making them more powerful
and customizable. Extensions can be written in JavaScript and interact with Jupyter’s
DOM and API. There are four main types of extensions:
1. Kernel Extensions: Enhance the computational kernel (e.g., supporting more
languages).
2. IPython Kernel Extensions: Enhance features specific to the IPython kernel (e.g.,
debugging tools).
3. Notebook Extensions: Modify the notebook’s interface (e.g., adding widgets or
improving layout).
4. Notebook Server Extensions: Extend server-side functionality (e.g., authentication
and logging).

Installing Extensions
jupyter nbextension install extension_name

jupyter nbextension enable 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").

Editable: Controls whether a cell can be edited (true/false).


Slide Type: Defines slide behavior in presentations (slide, subslide,
fragment, skip).

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.

Interesting features in Jupyter Notebooks:

1. Markdown Enhancements
• LaTeX Support: Incorporate mathematical formulas seamlessly within the text.

• Example: $$ E = mc^2 $$

• Inline LaTeX: Embed math expressions directly in text.

• Example: The equation is $E = mc^2$.


• Tables: Easily create tables in Markdown format.

• Example:
| Name | Age |
|------|-----|
| John | 28 |

• HTML Embedding: Embed raw HTML tags for enhanced formatting.

• 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)

4. Rich Cell Outputs


• Images: Display images within the notebook using Image("path").
• Rich Output Types: Support for rich media like HTML, SVG, and more.
• Example: display(HTML("<h2>Title</h2>"))
• Interactive Visualizations: Leverage libraries like Plotly or Bokeh for dynamic,
interactive plots.

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.

6. Save and Export Notebooks


• Export to HTML: Navigate to File → Download as → HTML.
• Export to PDF: Export as PDF via File → Download as → PDF (requires LaTeX
installation).
• Export as Slides: Export your notebook to Reveal.js slides through File → Download
as → Reveal.js slides.

7. Nbextensions (Notebook Extensions)


• Popular Extensions:
• Table of Contents: Automatically generates a table of contents for easy
navigation.
• Collapsible Headings: Make notebook headings collapsible for improved
organization.
• Code Folding: Allows folding and unfolding of code cells.
• Variable Inspector: Provides a panel showing all variables in the notebook’s
current session.
• Installation:
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

8. Persistent Variable Storage with joblib


• Save Models/Variables: Use joblib for efficient storage and retrieval of large Python
objects (e.g., machine learning models).
• Example:
import joblib
joblib.dump(my_model, 'model.pkl')
my_model = joblib.load('model.pkl')

9. Version Control with Git Integration


• nbdime: A tool for diffing and merging Jupyter notebooks.
• Installation: pip install nbdime

• Example:
nbdiff notebook1.ipynb notebook2.ipynb

10. Distributed Computing with IPython Parallel


• ipyparallel: Distribute computational tasks across multiple cores or machines.
• Installation: pip install ipyparallel
• Example:
from ipyparallel import Client
rc = Client()
dview = rc[:]

You might also like