Flask

From ElectroDragon Wiki
Revision as of 04:23, 6 February 2020 by Chao (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  • nohup python main.py &

Basic web

  • default port is 5000
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')



  • static file
from flask import Flask
from flask import render_template


app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template('test.html')

if __name__ =="__main__":
    app.run(host='0.0.0.0', debug=True, port=8090)


  • Use variables
from flask import Flask
app = Flask(__name__)

@app.route('/hello/<name>')
def hello_name(name):
   return 'Hello %s!' % name

if __name__ == '__main__':
   app.run(host='0.0.0.0', debug=True, port=8090)


  • Use variables
from flask import Flask
app = Flask(__name__)

@app.route('/hello/<name>/<int:date>/<int:share>/<float: price>')
def hello_name(name):
   return 'name %s, date: %s, share: %s, price: %s!' % (name, date, share, price)

if __name__ == '__main__':
   app.run(host='0.0.0.0', debug=True, port=8090)

uwsgi

  • python test1.py
  • uwsgi --socket 0.0.0.0:5000 --protocol=http -w test1:app

Reference