Home > Net >  Get user keyboard input in jupyter notebook
Get user keyboard input in jupyter notebook

Time:08-19

I want to ask user for confirmation before running an specific jupyter notebook cell. So far I'm trying to get the user's answer using input() but it's not working as the cell keeps running without doing nothing.

Update: I'm running the notebook on VSCode

This is the code i have

print('WARNING: this cell uses XXXX limited resources:')
answer = input('-> Are you sure you want to run it? [yes| No]: ' )
if answer == 'yes' :
   #run some computation

CodePudding user response:

Well, this code works just fine (tested in Notebook and in Lab):

print('WARNING: this cell uses XXXX limited resources:')
answer = input('-> Are you sure you want to run it? [yes| no]: ' )
if answer == 'yes':
    print("YES")
else:
    print("NO")
print("some other job")

If the answer is "yes" the code in if is executed. Otherwise it executes what's in else (in case you need it). The last line is executed no matter what the answer is.

CodePudding user response:

Leaving a comment inside if loop will create IndentationError: expected an indented block. Please try as follow,

updated code :

print('WARNING: this cell uses XXXX limited resources:')
answer = input('-> Are you sure you want to run it? [yes| No]: ' )
if answer == 'yes' :
   print("answer is YES")

output :

WARNING: this cell uses XXXX limited resources:
-> Are you sure you want to run it? [yes| No]: yes
answer is YES

Yet not solved, then update your ipykernel using,

pip install --upgrade ipykernel
  • Related