3

I understand there are forms, but not how to take a form input from one cell and use the input in other multiple cells.

Thank you!

I've looked into forms, but they only work for very basic code. I am trying to run a python program which means I have the cell code as

!python3 <nameofprogram>.py -flags arguments

The arguments are what I'd like to change, however even with a dropdown form selection, I can't get the code to understand that the argument is from a form, instead of taking the literal text which doesn't when I change the dropdown.

Here's the cell's code:

Section = "tracks" #@param ["tracks", "newalbums", "justreleased", "pop", "rock", "electronic", "country", "hiphop", "rnb", "kpop", "classical", "jazz", "latin", "holiday"]
!python3 redsea.py -a TV explore atmos Section

I see no answers that help me.

The final answer I found was to use $ before the piece in the code.

!python3 redsea.py -a TV explore atmos $Section

After asking for the input.

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
burntscarr
  • 75
  • 1
  • 1
  • 5

3 Answers3

6

You can prompt the user for input using the input function like so:

file_name = input('Enter the file name: ')
print(f'You entered {file_name}')

Here's a full example:

https://colab.research.google.com/drive/1OXsj6GqG76AK7DE-DUPo0zGOiiZjYwX0?usp=sharing

enter image description here

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
2

You can just use input().

name = input("Your name: ")

Then in another cell, you can use the name variable.

Here's a notebook to test.

korakot
  • 37,818
  • 16
  • 123
  • 144
0

The OP was asking how to use the Python input in shell commands (running with the !...).

The existing answers don't answer that question; the OP answered their own question; they use $python_var before the Python variable python_var (known as "variable expansion")

The existing answers suggest using input(...), which still requires using the $python_var if passing the Python variable into a shell (so you don't need to use input(...), you can use forms / Insert > Add a form field) to get the input in the first place (which is nice; forms have dropdowns and checkboxes and forms can "react" to your choice)...

Another option is to use {python_var} around the Python variable as described here and here (and here and here)

Example:

say = 'hi'
!echo {say}

Outputs:

hi

Another example that uses #forms (as the OP asked...)

#@title Choose a genre (auto-run) { run: "auto" }
genre = "newalbums" #@param ["tracks", "newalbums", "jazz", "latin", "holiday"]
!echo You picked genre: {genre}

Output (will re-run automatically as the user makes a different choice; see { run: "auto" } in the above)

You picked genre: newalbums

I think this syntax (for ! bang aka shell escaping, and the variable expansion aka interpolation) comes from IPython/Jupyter, which says:

The line after the bang can call any program installed in the underlying shell, and support variable expansion in the form of $variable or {variable}. The later form of expansion supports arbitrary python expressions:

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135