Carltonp
Carltonp

Reputation: 1344

How to run / execute input cells in Databricks Python Notebook

I written the following code in Databricks notebook

name = input("Please enter your name: ")
age = input("How old are you, {0}?".format(name))
print(age)

As you guessed, after running the cell I am asked to 'Please enter your name:' The problem is I don't where to make the entry. If this was written in intelliJ IDEA or IDLE I would be given a separate window to enter my name. However, with Databricks notebook, even if I enter the answer in a different cell it appears to be constantly waiting for an input, see image:

where to enter

I really should know the answer to this

Upvotes: 6

Views: 11702

Answers (2)

Bob Bricks
Bob Bricks

Reputation: 111

The input function is now natively supported in Databricks for DBR 11.2+.

An example notebook showing the use of input with pdb is available here: https://docs.databricks.com/_static/notebooks/python-debugger.html

A short screencast showing the execution of your example: enter image description here

Upvotes: 11

Fabio Schultz
Fabio Schultz

Reputation: 527

I think what you need is

dbutils.widgets.text("name", "Please enter your name")
dbutils.widgets.text("age", "How old are you?")

Look at the top of your notebook and you will look the textboxes to fill, fill it and execute another cell with this commands

name = dbutils.widgets.get("name")
print(name)
age = dbutils.widgets.get("age")
print(age)

Doc link https://docs.databricks.com/user-guide/notebooks/widgets.html#widget-types

Upvotes: 7

Related Questions