dting
dting

Reputation: 39287

Read values from another cell in IPython Notebook and supply them for "input()"

As an example, if I have a cell that looks like:

In [*]: for i in range(int(input())):
            print(i**2)

When the cell is run, an input box pops up and waits for input to be provided. Is there a Built-in magic command or any way to programmatically supply values when input() is encountered?

Upvotes: 0

Views: 558

Answers (1)

tamasgal
tamasgal

Reputation: 26269

I write this as an answer, although this is more or less a comment.

One "hacky" way is to overwrite input or make a generator which returns an input-function with a constant return value. So kind of mocking it…

def input_generator(return_value):
    def input():
        return return_value
    return input

This will work as:

>> input = input_generator(42)
>> input()
42

Upvotes: 1

Related Questions