Introduction to Jupyter Notebook
This tutorial teaches you how to install Jupyter Notebook in Windows 10. Organize your Python workflow on the web with Jupyter Notebook.
It supports:
- data science
- machine learning
- scientific computing
You can export a Jupyter Notebook in the following formats:
- HTML
- Markdown
- LaTeX
- Restructured Text
- Webpdf
- Executable Script
When you export as an Executable Script a code module is generated. There are other formats available.
A Jupyter Notebook is organized in lines. You write code inside a line and execute it interactively. In case of code errors, you can edit and execute the line again. This flexibility helps to save time and improve code editing skills.
You can move the lines up and down and also delete a line.
How to install Jupyter in Windows 10
Open a command prompt and update pip:
python -m pip install --upgrade pip
To install Jupyter type:
pip install jupyterlab
To avoid errors with openssl downgrade the urllib3 package:
pip uninstall urllib3 pip install urllib3==1.26.6
To find the Jupyter version type:
jupyter --version
To launch the Jupyter Notebook type:
jupyter lab
Then copy the url returned in the command prompt and paste it into a web browser.
http://localhost:8888/lab?token=c9673b1b2101fdc14b4c5c9ea736a5897de99c33820a412e
Create a new notebook in Jupyter
To start a new notebook click on the button like shown below.
![install jupyter in windows 10](http://awakenerd.com/wp-content/uploads/2024/04/jupyter_python_notebook_1.png)
Then type code on a new line.
![install jupyter in windows 10](http://awakenerd.com/wp-content/uploads/2024/04/jupyter_python_notebook_2.png)
To execute the code inside a line press SHIFT + Enter.
![install jupyter in windows 10](http://awakenerd.com/wp-content/uploads/2024/04/jupyter_python_notebook_3.png)
To save the notebook click on File, Save As, and type a name.
Create a configuration file for Jupyter
jupyter-lab is a command line tool shipped with Jupyter. It helps to:
- specify a browser
- generate the configuration file
- setup the IP and port that the server listens to
- specify the default directory for the notebooks to be saved
To learn more type:
jupyter-lab -h
To create a config for Jupyter type:
jupyter-lab --generate-config
The above command writes a default configuration file shown below.
Writing default config to: C:\Users\User\.jupyter\jupyter_lab_config.py
To change the default browser locate the following:
c.ServerApp.browser = ''
Type the full path of the browser’s executable:
c.ServerApp.browser = 'C:/Program Files/Google/Chrome/Application/chrome.exe %s'
Look at the above path. The / is used and a %s at the end.
To test the new browser type:
jupyter lab
To change the directory where notebooks are saved locate the following:
c.ServerApp.root_dir = ''
Put your absolute path.
c.ServerApp.root_dir = 'C:\\Users\\User\\Desktop\\data_science'
There are many other settings you can play with.
Install pandas and other data science’s libraries
To do data science you need specific libraries.
- matplotlib helps to plot data.
- pandas help to analyze and manipulate data.
- NumPy helps to work with arrays, matrices, linear algebra, and Fourier transform.
- SciPy is used for scientific computing.
- plotly is a browser-based graphing library.
- Scrapy helps to scrape data on the web.
- BeautifulSoup is used to parse HTML and XML documents.
- seaborn helps to make statistical graphics.
To install the libraries for data science with Python type:
pip install pandas matplotlib numpy scipy plotly scrapy beautifulsoup seaborn
The data science community uses many other libraries. Install the main ones and learn during the journey.
Create your first DataFrame in pandas
A DataFrame in pandas is treated as a dictionary. The column is the key and the row is the value.
To create your first DataFrame in pandas open a new notebook in Jupyter and type the following line by line:
The above example is taken from the book Thinking in Pandas.
Errors you may face while using Jupyter
403 forbidden when exporting a Jupyter Notebook
This error happens when the Origin header is missing in the HTTP request. To avoid it disable the XSRF check in the config:
c.ServerApp.disable_check_xsrf = True
The XSRF check protects against Cross-Site Request Forgery.
Final thoughts
Jupyter Notebook offers an interactive notebook interface to write Python. You can write code and watch it while executes. You can also save the code in notebooks, and organize and share it.