Flask/User Input
Appearance
< Flask
Running locally
[edit | edit source]static/index.html
[edit | edit source]<form name="form" method="post" action"" >
<label for="name">Enter your name:</label>
<input name="name" type="text">
<input type="submit" value="Submit">
</form>
main.py
[edit | edit source]from flask import Flask, request
app = Flask(__name__, static_url_path='')
@app.route('/', methods=['GET', 'POST'])
def root():
if request.method == "GET":
return app.send_static_file('./index.html')
elif request.method == "POST":
return "Hello " + request.form['name'] + "!"
else:
return "Unexpected request.method: " + request.method
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
Running in Repl.it
[edit | edit source]index.html
[edit | edit source]<form name="form" method="post" action"" >
<label for="name">Enter your name:</label>
<input name="name" type="text">
<input type="submit" value="Submit">
</form>
main.py
[edit | edit source]from flask import Flask
from flask import request
app = Flask(__name__, static_folder='.', root_path='/home/runner')
@app.route('/', methods=['GET', 'POST'])
def root():
if request.method == "GET":
return app.send_static_file('./index.html')
elif request.method == "POST":
return "Hello " + request.form['name'] + "!"
else:
return "Unexpected request.method: " + request.method
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000')
Try It
[edit | edit source]Copy and paste the code above into one of the following free online development environments or use your own User Input compiler / interpreter / IDE.