Google Colab - Adding Forms

Colab provides a very useful utility called Forms that allows you to accept inputs from the user at runtime. Let us now move on to see how to add forms to your notebook.

Adding Form

In an earlier lesson, you used the following code to create a time delay −

import time
print (time.ctime())

Suppose, you want a user set time delay instead of a fixed delay of 5 seconds. For this, you can add a Form to the Code cell to accept the sleep time.

Open a new notebook. Click on the Options (vertically-dotted) menu. A popup menu shows up as seen in the screenshot below −

Adding Form

Now, select Add a form option. It will add the form to your Code cell with a Default title as seen in the screenshot here −

Add Form Default Title

To change the title of the form, click on the Settings button (pencil icon on the right). It will pop up a settings screen as shown here:

Add Form Settings

Change the form title to “Form” and save the form. You may use some other name of your choice. Notice that it adds the @title to your code cell.

You may explore other options on the above screen at a later time. In the next section, we will learn how add input fields to the form.

Adding Form Fields

To add a form field, click the Options menu in the Code cell, click on the Form to reveal the submenus. The screen will look as shown below −

Adding Form Fields

Select Add a form field menu option. A dialog pops up as seen here −

Adding New Form Field

Leave the Form field type to input. Change the Variable name to sleeptime and set the Variable type to integer. Save the changes by clicking the Save button.

Your screen will now look like the following with the sleeptime variable added into the code.

Form Field Type

Next, let us see how to test the form by adding some code that uses the sleeptime variable.

Testing Form

Add a new Code cell underneath the form cell. Use the code given below −

import time
print (time.ctime())

You have used this code in the earlier lesson. It prints the current time, waits for a certain amount of time and prints a new timestamp. The amount of time that the program waits is set in the variable called sleeptime.

Now, go back to the Form Cell and type in a value of 2 for the sleeptime. Select the following menu −

Runtime / Run all

This runs the entire notebook. You can see an output screen as shown below.

Runs Entire Notebook

Notice that it has taken your input value of 2 for the


. Try changing this to a different value and Run all to see its effect.

Inputting Text

To accept a text input in your form, enter the following code in a new code cell.

name = 'Tutorialspoint' #@param {type:"string"}

Now, if you run the Code cell, whatever the name you set in the form would be printed on the screen. By default, the following output would appear on the screen.


Note that you may use the menu options as shown for the integer input to create a Text input field.

Dropdown List

To add a dropdown list to your form, use the following code −

color = 'green' #@param ["red", "green", "blue"]

This creates a dropdown list with three values - red, green and blue. The default selection is green.

The dropdown list is shown in the screenshot below −

Dropdown List

Date Input

Colab Form allows you to accept dates in your code with validations. Use the following code to input date in your code.

#@title Date fields
date_input = '2019-06-03' #@param {type:"date"}

The Form screen looks like the following.

Date Input

Try inputting a wrong date value and observe the validations.

So far you have learned how to use Colab for creating and executing Jupyter notebooks with your Python code. In the next chapter, we will see how to install popular ML libraries in your notebook so that you can use those in your Python code.

Kickstart Your Career

Get certified by completing the course

Get Started