Jupyter Notebook – Plotting

Jupyter Notebook – Plotting ”; Previous Next IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called. The show() function causes the figure to be displayed below in[] cell without out[] with number. Now, add plt.show() at the end and run the cell again to see the difference. Note that the %matplotlib notebook magic renders interactive plot. Just below the figure, you can find a tool bar to switch views, pan, zoom and download options. Importantly, if you modify the data underneath the plot, the display changes dynamically without drawing another plot. In the above example, change the data sets of x and y in the cell below and plot the figure again, the figure above will get dynamically refreshed. Print Page Previous Next Advertisements ”;

QtConsole – Getting Started

Jupyter QtConsole – Getting Started ”; Previous Next In this chapter, let us understand how to get started with QtConsole. This chapter will give you an overview about this software and explains its installation steps. Overview The Qt console is a GUI application similar to IPython terminal. However, it provides a number of enhancements which are not available in text based IPython terminal. The enhance features are inline figures, multi-line editing with syntax highlighting, graphical calltips, etc. The Qt console can use any Jupyter kernel, default being IPython kernel. Installation Jupyter QtConsole is a part of Project Jupyter. Anaconda distribution is already having QTconsole application in it. In order to install it individually, use pip command as shown below − pip3 install qtconsole You can also use the conda command for this purpose − conda install qtconsole You can start Jupyter console from Anaconda navigator. To start it from the command line, you should use the following command, either from the Windows command prompt or Anaconda prompt − jupyter qtonsole You get a terminal similar to IPython terminal with first In[] prompt. You can now execute any Python expression exactly like we do in IPython terminal or Jupyter notebook Print Page Previous Next Advertisements ”;

Converting Notebooks

Jupyter – Converting Notebooks ”; Previous Next Jupyter notebook files have .ipynb extension. Notebook is rendered in web browser by the notebook app. It can be exported to various file formats by using download as an option in the file menu. Jupyter also has a command line interface in the form of nbconvert option. By default, nbconvert exports the notebook to HTML format. You can use the following command for tis purpose − jupyter nbconvert mynotebook.ipynb This will convert mynotebook.ipynb to the mynotebook.html. Other export format is specified with `–to` clause. Note that other options include [”asciidoc”, ”custom”, ”html”, ”latex”, ”markdown”, ”notebook”, ”pdf”, ”python”, ”rst”, ”script”, ”slides”] HTML includes ”basic” and ”full” templates. You can specify that in the command line as shown below − jupyter nbconvert –to html –template basic mynotebook.ipynb LaTex is a document preparation format used specially in scientific typesetting. Jupyter includes ”base”, ”article” and ”report” templates. jupyter nbconvert –to latex –template report mynotebook.ipynb To generate PDF via latex, use the following command − jupyter nbconvert mynotebook.ipynb –to pdf Notebook can be exported to HTML slideshow. The conversion uses Reveal.js in the background. To serve the slides by an HTTP server, add –postserve on the command-line. To make slides that does not require an internet connection, just place the Reveal.js library in the same directory where your_talk.slides.html is located. jupyter nbconvert myslides.ipynb –to slides –post serve The markdown option converts notebook to simple markdown output. Markdown cells are unaffected, and code cells indented 4 spaces. –to markdown You can use rst option to convert notebook to Basic reStructuredText output. It is useful as a starting point for embedding notebooks in Sphinx docs. –to rst This is the simplest way to get a Python (or other language, depending on the kernel) script out of a notebook. –to script Print Page Previous Next Advertisements ”;

Cell Magic Functions

Jupyter Notebook – Cell Magic Functions ”; Previous Next In this chapter, let us understand cell magic functions and their functionalities. %%html This cell magic function renders contents of code cell as html script. %%js or %%javascript You can embed javascript code in Jupyter notebook cell with the help of this cell magic command. %%writefile Contents of code cell are written to a file using this command. Print Page Previous Next Advertisements ”;

Jupyter Notebook – User Interface

Jupyter Notebook – User Interface ”; Previous Next In the user interface of Jupyter, just beside the logo in the header, the file name is displayed. You can find the menu bar below the header. Each menu contains many options that will be discussed later. A row of icons forming toolbar helps user to perform often required operations The notebook has two modes − Command mode and Edit mode. Notebook enters edit mode when a cell is clicked. Notice the pencil symbol just besides name of kernel. Kernel indicator symbol is displayed just to the right of kernel name. Note that a hollow circle means kernel is idle and solid circle means it is busy. File Menu The following are the options available in the File menu − Sr.No. File menu & Description 1 New notebook choose the kernel to start new notebook 2 Open Takes user to dashboard to choose notebook to open 3 Save as save current notebook and start new kernel 4 Rename rename current notebook 5 Save saves current notebook and stores current checkpoint 6 Revert reverts state of notebook to earlier checkpoint 7 Download export notebook in one of various file formats The file formats that are available are shown below − Edit Menu Edit menu consists of buttons to perform cut, copy and paste cells, delete selected cell, split and merge cells, move cells up and down, find and replace within notebook, cut/copy attachments and insert image. View Menu Buttons in this menu help us to hide/display header, toolbar and cell numbers. Insert Menu This menu gives you options for inserting cell before or after the current cell. Cell Menu The options in this menu let user run all or specific cells in the notebook. You can also set the cell type to code type, markdown or raw nbconvert type. Kernel Menu From this menu you can start, interrupt, restart or shutdown the kernel. You can also start a new kernel. Widgets Menu From this menu you can save, clear, download or embed widget state. Help menu Various predefined keyboard shortcuts are displayed from this menu. You can also edit the shortcuts as per your convenience. Print Page Previous Next Advertisements ”;

Connecting to Jupyter Notebook

Connecting to Jupyter Notebook ”; Previous Next There is a %qtconsole magic command available for use with Jupyter notebook. This invokes the QtConsole as a slave terminal to notebook frontend. As a result, data between notebook and Qtconsole terminal can be shared. You can see that the variable in notebook is accessible within qtconsole window. Also, a new variable in Qtconsole is used back in notebook. Observe that the input and output cells are numbered incrementally between the two. Print Page Previous Next Advertisements ”;

QtConsole – Save to Html

Jupyter QtConsole – Save to HTML ”; Previous Next This option to save the QtConsole output as HTML file is available in File menu. You can choose to create file with inline image or the plotted figure as external png file in an adjacent folder (named as qt_files). Print Page Previous Next Advertisements ”;

QtConsole – Multiline Editing

Jupyter QtConsole – Multiline Editing ”; Previous Next Multiline editing is one of the features which is not available in IPython terminal. In order to enter more than one statements in a single input cell, press ctrl+enter after the first line. Subsequently, just pressing enter will go on adding new line in the same cell. To stop entering new lines and running cell, press enter key one more time at the end. The cell will run and output will be displayed in next out[] cell. Print Page Previous Next Advertisements ”;

QtConsole – Inline Graphics

Jupyter QtConsole – Inline Graphics ”; Previous Next Another important enhancement offered by QtConsole is the ability to display inline graphics, especially plots. The feature works well with Matplotlib as well as other plotting libraries. Print Page Previous Next Advertisements ”;

IPython – IO Caching

IPython – IO Caching ”; Previous Next The input and output cells on IPython console are numbered incrementally. In this chapter, let us look into IO caching in Python in detail. In IPython, inputs are retrieved using up arrow key. Besides, all previous inputs are saved and can be retrieved. The variables _i, __i, and ___i always store the previous three input entries. In addition, In and _in variables provides lists of all inputs. Obviously _in[n] retrieves input from nth input cell. The following IPython session helps you to understand this phenomenon − In [1]: print (“Hello”) Hello In [2]: 2+2 Out[2]: 4 In [3]: x = 10 In [4]: y = 2 In [5]: pow(x,y) Out[5]: 100 In [6]: _iii, _ii, _i Out[6]: (”x = 10”, ”y = 2”, ”pow(x,y)”) In [7]: In Out[7]: [””, ”print (“Hello”)”, ”2+2”, ”x = 10”, ”y = 2”, ”pow(x,y)”, ”_iii, _ii, _i”, ”In” ] In [8]: In[5] 9. IPython — IO Out[8]: ”pow(x,y)” In [9]: _ih Out[9]: [””, ”print (“Hello”)”, ”2+2”, ”x = 10”, ”y = 2”, ”pow(x,y)”, ”_iii, _ii, _i”, ”In”, ”In[5]”, ”_ih” ] In [11]: _ih[4] Out[11]: ”y = 2” In [12]: In[1:4] Out[12]: [”print (“Hello”)”, ”2+2”, ”x=10”] Similarly, single, double and triple underscores act as variables to store previous three outputs. Also Out and _oh form a dictionary object of cell number and output of cells performing action (not including assignment statements). To retrieve contents of specific output cell, use Out[n] or _oh[n]. You can also use slicing to get output cells within a range. In [1]: print (“Hello”) Hello In [2]: 2+2 Out[2]: 4 In [3]: x = 10 In [4]: y = 3 In [5]: pow(x,y) Out[5]: 1000 In [6]: ___, __, _ Out[6]: (””, 4, 1000) In [7]: Out Out[7]: {2: 4, 5: 1000, 6: (””, 4, 1000)} In [8]: _oh Out[8]: {2: 4, 5: 1000, 6: (””, 4, 1000)} In [9]: _5 Out[9]: 1000 In [10]: Out[6] Out[10]: (””, 4, 1000) Print Page Previous Next Advertisements ”;