4

Essentially what I am trying to do :

I have a simple HTML page with a Textarea to input a bunch of text, my use case is a single code on each line like below:

1234
5678
1456
etc.

Ideally I want to take that into Python and be able to work with the data and return the results. So lets start simple and say take each line as a separate entry and run it against a function to add the word "Hi" in front of it So the results are:

Hi 1234
Hi 5678
etc.

So far have this working example I found but I tend to break it anytime I try something.

Html:

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Enter some text</h1>
   <form action="submit" id="textform" method="post">
    <textarea name="text">Hello World!</textarea>
    <input type="submit" value="Submit">
</form>
</body>
</html>

Python:

From flask import Flask, request
app = Flask(__name__)

@app.route('/')
def main_form():
    return '<form action="submit" id="textform" method="post"><textarea name="text">Hello World!</textarea><input type="submit" value="Submit"></form>'

@app.route('/submit', methods=['POST'])
def submit_textarea():
    return "You entered: {}".format(request.form["text"])

if __name__ == '__main__':
    app.run()

Example :

i try to extract the textarea to a string and then return that back to the page with :

x = format(request.form["text"]) 
return x

Any help or guidance would be appreciated!

PRMoureu
  • 12,817
  • 6
  • 38
  • 48
JSimonsen
  • 2,642
  • 1
  • 13
  • 13
  • You said you have tried something and it broke? Please show that code to us! – Klaus D. Oct 17 '17 at 00:23
  • Essentially anything trying to extract the textarea to a string and then return that back to the page. Example: x = format(request.form["text"]) return x. – JSimonsen Oct 17 '17 at 02:50

1 Answers1

6

You can access and store the text from textarea with the following lines :

@app.route('/submit', methods=['POST'])
def submit_textarea():
    # store the given text in a variable
    text = request.form.get("text")

    # split the text to get each line in a list
    text2 = text.split('\n')

    # change the text (add 'Hi' to each new line)
    text_changed = ''.join(['<br>Hi ' + line for line in text2])
    # (i used <br> to materialize a newline in the returned value)

    return "You entered: {}".format(text_changed)
PRMoureu
  • 12,817
  • 6
  • 38
  • 48