= input("Please enter a stock symbol (e.g. 'MSFT'): ")
symbol print(symbol)
Please enter a stock symbol (e.g. 'MSFT'): NVDA
NVDA
When creating data dashboards and user facing applications, it may be helpful to ask the user for inputs.
Besides capturing a textual inputs using the input
function, we can use a form to capture a variety of other kinds of inputs.
To implement a form in Google Colab, we can use Colab Form Inputs, or leverage the capabilities of the Jupyter Widgets package.
input
Function= input("Please enter a stock symbol (e.g. 'MSFT'): ")
symbol print(symbol)
Please enter a stock symbol (e.g. 'MSFT'): NVDA
NVDA
getpass
ModuleWhen using the input
function in Python to capture user inputs, we end up seeing the resulting value as the user types it.
But what if we want to ask for more sensitive inputs, like secret passwords or credentials? We need a way to prevent them from showing up in the program’s output.
To hide or “mask” user-inputted values, we can use the getpass
function from the getpass
module instead:
from getpass import getpass
= getpass("Please input your secret password: ")
my_password print(my_password)
Please input your secret password: ··········
super-secret
One helpful way to create form inputs is to add the “Adding form fields” code snippet, which provides example form fields of various kinds. Adding the snippet yeilds the following example form:
# @title Example form fields
# @markdown Forms support many types of fields.
= '' # @param
no_type_checking = 'example' # @param {type: "string"}
string_type = 142 # @param {type: "slider", min: 100, max: 200}
slider_value = 102 # @param {type: "number"}
number = '2024-06-23' # @param {type: "date"}
date = "monday" # @param ['monday', 'tuesday', 'wednesday', 'thursday']
pick_me = "apples" # @param ["apples", "bananas", "oranges"] {allow-input: true}
select_or_input # @markdown ---
print(slider_value)
print(date)
142
2024-06-23
We see the form inputs are implemented by structured comments to the right of the variable assignment.
In practice, we’ll probably only need one or two of these, and we can copy the corresponding code snippet and adapt it to fit the use case.
For example, creating a drop-down menu prompting a user to select a stock symbol:
= "NVDA" # @param ['MSFT', 'GOOGL', 'NVDA', 'SPOT', 'NFLX']
symbol print(symbol)
NVDA
To make the Colab form inputs work, we first choose an option from the form on the right. This updates the code cell contents on the left. Then we run the cell to store the selected value in memory.
You’ll notice the Colab form approach requires us to hard-code the selectable options in code comments. This may work in many cases, but there may be some situations where you would want to dynamically compile the list of options instead.
There’s another option for creating form inputs in a Python notebook, using the Jupyter Widgets (ipywidgets
) package.
Compiling dropdown selections from a list:
= ['MSFT', 'GOOGL', 'NVDA', 'SPOT', 'NFLX']
symbols print(symbols)
['MSFT', 'GOOGL', 'NVDA', 'SPOT', 'NFLX']
from ipywidgets.widgets import Dropdown
# configure the dropdown menu:
= Dropdown(
dropdown =symbols,
options=symbols[0], # default value
value='Symbol: ',
description=False,
disabled
)
# display the dropdown menu:
display(dropdown)
Using the selected value:
print(dropdown.value)
NVDA
To make the Jupyter Widgets work in this simple use case, we first choose an option from the form. This updates the selected value displayed within the dropdown menu. Then we can reference the currently selected value.