Tuesday, February 16, 2021

Jupyter Notebook

There are various ways to run Python statements. We can run Python statements in a script and in the interpreter’s interactive mode. The main advantage of using interactive mode is the immediate feedback. The main disadvantage of this mode is that, if we make any mistakes in typing the statements and then execute the erroneous statement, we must rewrite the entire statement to re-execute it. It is also difficult to save it as a program. The option for saving the statements run on the interpreter can be found under the File menu option. However, all the statements and their outputs will be saved in plain text format with a .py extension. If there is any graphical output, it is displayed separately and cannot be stored along with the statements.

Owing to these limitations of interactive mode with interpreter, we will use a better tool for running the Python statements interactively in the web browser: Jupyter Notebook. Jupyter is a server program that can create interactive notebooks in a web browser.

A Jupyter notebook is a web-based notebook that is used for interactive programming with various languages, including Python, Octave, Julia, and R. It is very popular with people working in research domains. A Jupyter notebook can have code, visualizations, output, and rich text in a single file. The advantage of Jupyter Notebook over Python’s own interactive prompt is that, users can edit the code and see the new output instantly, which is not possible in Python interactive mode. Another advantage is that we have the code, rich text elements, and the code’s output (which could be in graphical or rich text format) in the same file on the disk. This makes it easy to distribute. We can save and share these notebooks over the Internet or using portable storage equipment. There are many services online that help us to store and execute Jupyter Notebook scripts on cloud servers.

We can easily install the Jupyter Notebook server program on any computer by running the following command at the command prompt:

pip3 install jupyter

Let’s explore how we can use Jupyter Notebook for writing and executing Python statements. Run the following command at the OS command prompt to launch the Jupyter Notebook server process there: 

jupyter notebook

Once the Jupyter notebook server process is launched, the command prompt window shows a server log, as displayed in Figure below:

 

It launches a web page in the default browser in the OS. If the browser window is already open, it launches the page on a new tab of the same browser window. Another way to open the page (in case you accidentally close this browser window) is to visit http://localhost:8888/ in your browser, which displays a page similar to the one shown in Figure below:

 

The token can be found in the server logs. The following is a sample server log with tokens.
To access the notebook, open this file in a browser:

file:///C:/Users/Ashwin/AppData/Roaming/jupyter/runtime/nbserver-8420-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c 

or http://
127.0.0.1:8888/?token=e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c

In this log, we can see a couple of URLs. They refer to the same page (localhost and 127.0.0.1 are the same hosts). We can either copy and paste any of these URLs directly in the address bar of the browser tab and open the Jupyter Notebook home page or we can visit http://localhost:8888/ as previously mentioned and then paste the token in the server log (in our case it is e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c) and log in, which will take us to the same home page.

Note that every instance of the Jupyter Notebook server will have its own token and the token here will not work with your Jupyter Notebook. The token is only valid for that server process. After you follow any one of the routes just explained, you will see a home page tab in the browser window that looks like Figure shown below:

 

So just install this Jupyter Notebook and play with it to understand it's functionality.

Share:

0 comments:

Post a Comment