How to write and run a program in Jupyter lab?

How to write and run a program in Jupyter

After renaming the file, click on the first cell in the notebook to enter in the edit mode. Now you can write the code in working area. After writing the code, you can run it by pressing the Shift+ Enter key or directly click on the run button at the top of the screen.

Types of cells in Jupyter Notebook

There are the following four types of cells used in the Jupyter Notebook.

1. Code Cell

The contents present in a code cell is treated as statements in a programming language of the current kernel. By default, Jupyter notebook’s kernel is in Python so you can write Python statements in a code cell. When you run the statement, its output is displayed below the code. Output can be presented in the form of text, image, matplotlib plots, or HTML tables.

2. Markdown Cell

Markdown cell provides documentation to the notebook and makes the notebook more attractive. This cell contains all types of formatting features such as making text bold and italic, headers, displaying ordered or unordered list, Bullet lists, Hyperlinks, tabular contents, images, etc.

To perform the following formatting features, first select Markdown cell from the drop-down menu.

Bold and Italics

  • To make text bold, write text between the double underscores or double asterisks .

Types of cells in Jupyter Notebook

The following screenshot shows the output of the above code.

Types of cells in Jupyter Notebook

  • To make text italics, write text between single underscore or single asterisk .

Types of cells in Jupyter Notebook

The following screenshot shows the output of the above code.

Types of cells in Jupyter Notebook

Headers

Creating headers in Markdown is quite similar to the creating headers in HTML. It displays text in 6 sizes. To make the text as a header, start the text using # symbol. The number of # symbols depends upon the size of the header.

For example -

Header 1 use one # symbol, header 2 use two # symbol, and so on.

Types of cells in Jupyter Notebook

The following screenshot shows the output of the above Header cells.

Types of cells in Jupyter Notebook

Ordered Lists

The ordered list starts with 1. Use tab to make the suborder followed by the order.

Types of cells in Jupyter Notebook

The following screenshot shows the output of the above Markdown data.

Types of cells in Jupyter Notebook

Bullet lists

In Jupyter notebook, if text starts with the dash (-) symbol, markdown cell coverts dash into a solid circle and asterisk (*) to a solid square .

Types of cells in Jupyter Notebook

The following screenshot shows the output of the above Markdown data.

Types of cells in Jupyter Notebook

Hyperlinks

Markdown cell allows you to attach the Hyperlink. To attach the hyperlink place the name of the link in square brackets [] and write link inside the parentheses () .

You can use the following code to insert the hyperlink.

Types of cells in Jupyter Notebook

Output:

Types of cells in Jupyter Notebook

Table Content

Markdown cell allows you to create a table using pipe symbol (|) and dash symbol (-) . Pipe symbol (|) is used for making columns, and dash symbol (-) is used for making the rows.

The table creation is shown below:

Types of cells in Jupyter Notebook

The following screenshot shows the table content of markdown cell.

Types of cells in Jupyter Notebook

Images

To insert the image in a markdown cell, you first need to insert the image in the same directory. For this, go to Jupyter dashboard -> select Upload , specify the path of an image then click on Open .

Once the image is seen in the dashboard click on the Upload, you can see that image is uploaded in the dashboard.

Types of cells in Jupyter Notebook

Now, go to your current Notebook, and type the following code to insert the image.

Types of cells in Jupyter Notebook

The following screenshot shows that the image is inserted on the Notebook.

Types of cells in Jupyter Notebook

3. Raw NBConvert Cell

Raw NBConvert Cell provides a place where you can write output directly. These cells are not evaluated by the notebook kernel.

4. Heading Cell

The Jupyter Notebook does not support the heading cell. When you select the Heading from the drop-down menu, a pop will open on the screen which is shown in the below screenshot.

Types of cells in Jupyter Notebook

IPyWidgets in the Jupyter Notebook

The ipywidgets provides many common user interfaces for exploring code and data interactively.

By default, ipywidgets are installed in Anaconda or you can also install it manually with conda.

Some example of ipywidges are given below:

1. Text widget

The text widget allows the user to write the String:

from ipywidgets import widgets  
title_textbox = widgets.Text(  
    value = 'Hello World',  
    description = 'Title:',  
)  
title_textbox  

Output:

IPyWidgets in the Jupyter Notebook

2. Button widget

The button widget is similar to the HTML button. To create button, type the following code.

from ipywidgets import widgets  
button = widgets.Button(  
    description='Press Me',  
)  
button  

Output:

IPyWidgets in the Jupyter Notebook

3. color picker

The Color picker allows you to select a color as per to your requirement.

from ipywidgets import widgets  
color_picker = widgets.ColorPicker(  
    concise = True,  
    description = 'Background color:',  
    value = '#efefef',  
)  
color_picker  

Output:

IPyWidgets in the Jupyter Notebook

When you click on the square box, the following pop up will open.

IPyWidgets in the Jupyter Notebook

4. Slider

Sider is used to find the range and interval between two entities.

from ipywidgets import widgets  
range_slider = widgets.FloatRangeSlider(  
    value = [-2., +10.],  
    min = -10., max = +20., step = 0.1,  
    description = 'range:',  
    readout_format = '.1f',  
)  
range_slider  

Output:

IPyWidgets in the Jupyter Notebook

Example: Write a code to add two numbers.

from ipywidgets import widgets  
lb1 = widgets.Label('Enter the First number')  
display(lb1)  
text1 = widgets.Text()  
display(text1)  
lb2 = widgets.Label('Enter the Second number')  
display(lb2)  
text2 = widgets.Text()  
display(text2)  
btn = widgets.Button(description = "add")  
display(btn)  
lb3 = widgets.Label()  
display(lb3)  
def add(x):  
    a = int(text1.value)  
    b = int(text2.value)  
    lb3.value = 'result='+str(a+b)  
btn.on_click(add)  

Output:

IPyWidgets in the Jupyter Notebook