Jupyter Notebook – Editing

Jupyter Notebook – Editing ”; Previous Next While the menu bar and toolbar lets you perform various operations on notebook, it is desirable to be able to use keyboard shortcuts to perform them quickly. Jupyter Notebooks have two different keyboard input modes − Command Mode − Binds the keyboard to notebook level actions. Indicated by a grey cell border with a blue left margin. Edit Mode − When you are typing in a cell. Indicated by a green cell border. Command Mode (press Esc to enable) F find and replace 1 change cell to heading 1 Ctrl-Shift-F open the command palette 2 change cell to heading 2 Ctrl-Shift-P open the command palette 3 change cell to heading 3 Enter enter edit mode 4 change cell to heading 4 P open the command palette 5 change cell to heading 5 Shift-Enter run cell, select below 6 change cell to heading 6 Ctrl-Enter run selected cells A insert cell above Alt-Enter run cell and insert below B insert cell below Y change cell to code X cut selected cells M change cell to markdown C copy selected cells R change cell to raw V paste cells below K select cell above Z undo cell deletion Up select cell above D,D delete selected cells Down select cell below Shift-M merge selected cells, or current cell with cell below if only one cell is selected J select cell below Shift-V paste cells above Shift-K extend selected cells above L toggle line numbers Shift-Up extend selected cells above O toggle output of selected cells Shift-Down extend selected cells below Shift-O toggle output scrolling of selected cells Shift-J extend selected cells below I,I interrupt the kernel Ctrl-S Save and Checkpoint 0,0 restart the kernel (with dialog) S Save and Checkpoint Esc close the pager Shift-L toggles line numbers in all cells, and persist the setting Q close the pager Shift-Space scroll notebook up Space scroll notebook down Edit Mode (press Enter to enable) Tab code completion or indent Ctrl-Home go to cell start Shift-Tab tooltip Ctrl-Up go to cell start Ctrl-] indent Ctrl-End go to cell end Ctrl-[ dedent Ctrl-Down go to cell end Ctrl-A select all Ctrl-Left go one word left Ctrl-Z undo Ctrl-Right go one word right Ctrl-/ comment Ctrl-M enter command mode Ctrl-D delete whole line Ctrl-Shift-F open the command palette Ctrl-U undo selection Ctrl-Shift-P open the command palette Insert toggle overwrite flag Esc enter command mode Ctrl-Backspace delete word before Ctrl-Y redo Ctrl-Delete delete word after Alt-U redo selection Shift-Enter run cell, select below Ctrl-Shift-Minus split cell at cursor Ctrl-Enter run selected cells Down move cursor down Alt-Enter run cell and insert below Up move cursor up Ctrl-S Save and Checkpoint Print Page Previous Next Advertisements ”;

Jupyter Notebook – Dashboard

Jupyter Notebook – Dashboard ”; Previous Next The dashboard of Jupyter Notebook contains three tabs as shown in the screenshot given below − Files Tab The “Files” tab displays files and folders under current directory from which notebook app was invoked. The row corresponding to a notebook which is currently open and the running status is shown just beside the last modified column. It also displays Upload button using which a file can be uploaded to notebook server. Running Tab The “Running” tab shows which of the notebooks are currently running. Cluster Tab The third tab, “Clusters”, is provided by IPython parallel. IPython”s parallel computing framework, an extended version of the IPython kernel. From the New dropdown choose Terminal to open a cmd window. You can now start an IPython terminal here. Print Page Previous Next Advertisements ”;

Project Jupyter – Overview

Project Jupyter – Overview ”; Previous Next Project Jupyter started as a spin-off from IPython project in 2014. IPython’s language-agnostic features were moved under the name – Jupyter. The name is a reference to core programming languages supported by Jupyter which are Julia, Python and RProducts under Jupyter project are intended to support interactive data science and scientific computing. The project Jupyter consists of various products described as under − IPykernel − This is a package that provides IPython kernel to Jupyter. Jupyter client − This package contains the reference implementation of the Jupyter protocol. It is also a client library for starting, managing and communicating with Jupyter kernels. Jupyter notebook − This was earlier known as IPython notebook. This is a web based interface to IPython kernel and kernels of many other programming languages. Jupyter kernels − Kernel is the execution environment of a programming language for Jupyter products. The list of Jupyter kernels is given below − Kernel Language URL IJulia Julia https://github.com/JuliaLang IHaskell Haskell https://github.com/gibiansky IRuby Ruby https://github.com/SciRuby/iruby IJavaScript JavaScript https://github.com/n-riesco IPHP PHP https://github.com/dawehner IRKernel R http://irkernel.github.io/ Qtconsole − A rich Qt-based console for working with Jupyter kernels nbconvert − Converts Jupyter notebook files in other formats JupyterLab − Web based integrated interface for notebooks, editors, consoles etc. nbviewer − HTML viewer for notebook files 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 ”;

Importing Python Shell Code

IPython – Importing Python Shell Code ”; Previous Next IPython can read from standard Python console with default >>> prompt and another IPython session. The following screenshot shows a for loop written in standard Python shell − Copy the code (along with Python prompt) and paste the same in IPython input cell. IPython intelligently filters out the input prompts (>>> and …) or IPython ones (In [N]: and …:) Similarly, code from one IPython session can be pasted in another. The first screenshot given below shows definition of SayHello() function in one IPython window − Now, let us select the code and paste in another IPython shell and call SayHello() function. Print Page Previous Next Advertisements ”;

IPython – Embedding IPython

Embedding IPython ”; Previous Next The embed() function of IPython module makes it possible to embed IPython in your Python codes’ namespace. Thereby you can leverage IPython features like object introspection and tab completion, in default Python environment. Python objects present in the global namespace before embedding, will be available to IPython. If new objects are formed while in IPython or previous objects are modified, they will be automatically available to default environment after exiting IPython. Embedded IPython shell doesn’t change the state of earlier code or objects. However, if IPython is embedded in local namespace like inside a function, the objects inside it will not be available once it is closed. Here, we have defined a function add(). Inside add() we invoke IPython and declared a variable. If we try to access variable in IPython after it is closed, NameError exception will be raised. Print Page Previous Next Advertisements ”;

Jupyter Notebook – Types of Cells

Jupyter Notebook – Types of Cells ”; Previous Next Cells in Jupyter notebook are of three types − Code, Markdown and Raw. Code Cells Contents in this cell are treated as statements in a programming language of current kernel. Default kernel is Python. So, we can write Python statements in a code cell. When such cell is run, its result is displayed in an output cell. The output may be text, image, matplotlib plots or HTML tables. Code cells have rich text capability. Markdown Cells These cells contain text formatted using markdown language. All kinds of formatting features are available like making text bold and italic, displaying ordered or unordered list, rendering tabular contents etc. Markdown cells are especially useful to provide documentation to the computational process of the notebook. Raw Cells Contents in raw cells are not evaluated by notebook kernel. When passed through nbconvert, they will be rendered as desired. If you type LatEx in a raw cell, rendering will happen after nbconvert is applied. Print Page Previous Next Advertisements ”;

Installation and Getting Started

Installation and Getting Started ”; Previous Next You can easily install Jupyter notebook application using pip package manager. pip3 install jupyter To start the application, use the following command in the command prompt window. c:python36>jupyter notebook The server application starts running at default port number 8888 and browser window opens to show notebook dashboard. Observe that the dashboard shows a dropdown near the right border of browser with an arrow beside the New button. It contains the currently available notebook kernels. Now, choose Python 3, then a new notebook opens in a new tab. An input cell as similar to that of in IPython console is displayed. You can execute any Python expression in it. The result will be displayed in the Out cell. Print Page Previous Next Advertisements ”;

Jupyter Notebook – Markdown Cells

Jupyter Notebook – Markdown Cells ”; Previous Next Markdown cell displays text which can be formatted using markdown language. In order to enter a text which should not be treated as code by Notebook server, it must be first converted as markdown cell either from cell menu or by using keyboard shortcut M while in command mode. The In[] prompt before cell disappears. Header cell A markdown cell can display header text of 6 sizes, similar to HTML headers. Start the text in markdown cell by # symbol. Use as many # symbols corresponding to level of header you want. It means single # will render biggest header line, and six # symbols renders header of smallest font size. The rendering will take place when you run the cell either from cell menu or run button of toolbar. Following screenshot shows markdown cells in edit mode with headers of three different levels. When cells are run, the output is as follows − Note that Jupyter notebook markdown doesn’t support WYSWYG feature. The effect of formatting will be rendered only after the markdown cell is run. Ordered Lists To render a numbered list as is done by <ol> tag of HTML, the First item in the list should be numbered as 1. Subsequent items may be given any number. It will be rendered serially when the markdown cell is run. To show an indented list, press tab key and start first item in each sublist with 1. If you give the following data for markdown − It will display the following list − Bullet lists Each item in the list will display a solid circle if it starts with – symbol where as solid square symbol will be displayed if list starts with * symbol. The following example explains this feature − The rendered markdown shows up as below − Hyperlinks Markdown text starting with http or https automatically renders hyperlink. To attach link to text, place text in square brackets [] and link in parentheses () optionally including hovering text. Following screenshot will explain this. The rendered markdown appears as shown below − Bold and Italics To show a text in bold face, put it in between double underscores or two asterisks. To show in italics, put it between single underscores or single asterisks. The result is as shown below − Images To display image in a markdown cell, choose ‘Insert image’ option from Edit menu and browse to desired image file. The markdown cell shows its syntax as follows − Image will be rendered on the notebook as shown below − Table In a markdown cell, a table can be constructed using | (pipe symbol) and – (dash) to mark columns and rows. Note that the symbols need not be exactly aligned while typing. It should only take respective place of column borders and row border. Notebook will automatically resize according to content. A table is constructed as shown below − The output table will be rendered as shown below − Print Page Previous Next Advertisements ”;

Jupyter Notebook – Introduction

Jupyter Notebook – Introduction ”; Previous Next IPython notebook was developed by Fernando Perez as a web based front end to IPython kernel. As an effort to make an integrated interactive computing environment for multiple language, Notebook project was shifted under Project Jupyter providing front end for programming environments Juila and R in addition to Python. A notebook document consists of rich text elements with HTML formatted text, figures, mathematical equations etc. The notebook is also an executable document consisting of code blocks in Python or other supporting languages. Jupyter notebook is a client-server application. The application starts the server on local machine and opens the notebook interface in web browser where it can be edited and run from. The notebook is saved as ipynb file and can be exported as html, pdf and LaTex files. Print Page Previous Next Advertisements ”;