4

I have a "form" where I have a few raw_inputs to get the user response.

Now I want them to be pre-initialized with a default value. Is there any way to fill these raw_input fields? Or is there a good alternative to raw_input where that is possible?

To clarify, I have something looking like this:

val = raw_input("Input val:")

What I want as output is the following:

Input val: default value

and I want the user to be able to erase or edit the default value as if they had written it themselves. Is there any way to do that?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Dakkaron
  • 5,930
  • 2
  • 36
  • 51
  • What do you mean a form with raw_input? They are usually two different things. In general, you can use e.g. `user_input = raw_input() or default_value`, but it's not totally clear what you're asking for. – jonrsharpe Feb 18 '15 at 16:40
  • I edited the question to show what I mean – Dakkaron Feb 18 '15 at 16:49
  • 1
    You can't do that sort of thing with `raw_input`, no. You can tell the user what the default will be (per Elizion's answer, although I would factor out the default value to reduce duplication), but they can't interact with the prompt text. – jonrsharpe Feb 18 '15 at 16:51
  • Found a way to do it, see my answer. – Dakkaron Feb 19 '15 at 11:30
  • Possible duplicate of [How to set a default editable string for raw\_input?](https://stackoverflow.com/questions/5403138/how-to-set-a-default-editable-string-for-raw-input) – LightCC Oct 10 '19 at 06:34

3 Answers3

3

raw_input is not like a web form. The name should give it away: all the function does is accept raw input (in the form of a string), optionally printing a prompt first. If you want a default value, you'll need to set it yourself as in Elizion's answer.


Now, that said, you can hack the input prompt to look sort of like what you want. I do not recommend this at all, but for the sake of completeness, give the code below a try in an interactive console:

default_val = 'default'
i = raw_input('Input val: {}\rInput val: '.format(default_val)) or default_val

Here I'm abusing the \r character to move the cursor back to the start of the prompt. Then I re-write the actual prompt part, leaving the cursor on the d character, so that the user can type "over" it.

You'll notice though that "default" is still displayed there until you've typed over all of it (at which point it's probably gone for good, depending on your console's behavior). But it's only displayed; not actually part of the input. If the user enters "foo" (which will make the prompt look like this: Input val: fooult), i will get the value of "foo". If the user just presses Enter without entering text, i will be the empty string, which is why the or default_val is still necessary in the code. All this is to say that this is almost certainly not what you want to do. If you want a web form, make a web form. raw_input (and for that matter, console I/O) was not made for this.

Community
  • 1
  • 1
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • Actually, I found a way to do that cleanly. Have a look at my answer. Thanks for your answer though, I like the hack ;) – Dakkaron Feb 19 '15 at 11:31
3

I actually found an answer myself after some more Googling. It can be done with raw_input when using the readline module as follows:

import readline

def pre_input_hook():
    readline.insert_text('DefaultValue')
    readline.redisplay()

readline.set_pre_input_hook(pre_input_hook)

while True:
    line = raw_input('Prompt ("stop" to quit): ')
    if line == 'stop':
        break
    print 'ENTERED: "%s"' % line

Or, to wrap it all in an easier-to-handle function based on jonrsharpe's comment:

import readline

DEFAULT_TEXT = ''

def default_hook():
    """Insert some default text into the raw_input."""
    readline.insert_text(default_hook.default_text)
    readline.redisplay()

readline.set_pre_input_hook(default_hook)

def raw_input_default(prompt, default=None):
    """Take raw_input with a default value."""
    default_hook.default_text = DEFAULT_TEXT if default is None else default
    return raw_input(prompt)
Community
  • 1
  • 1
Dakkaron
  • 5,930
  • 2
  • 36
  • 51
1

You can set the raw_input()'s like this such that if the user doesn't enter anything (just hits enter) the value will be set to "my_default":

i = raw_input("Input Something[my_default]:") or "my_default"

EDIT:: Regarding your question update, as was pointed out in the comments you cannot have that sort of user interaction using raw_input().

HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34