Flask

From ElectroDragon Wiki
  • 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