Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Flask Template

20 Apr 2020 » template
from flask import Flask, request, jsonify, json, abort, redirect, url_for, render_template, send_file
from flask_cors import CORS, cross_origin
import os
import re
import subprocess
import traceback
import werkzeug
from werkzeug.routing import PathConverter

app = Flask(__name__, template_folder='template')
cors = CORS(app)

class EverythingConverter(PathConverter):
    regex = '.*?'
    part_isolating = False

app.url_map.converters['everything'] = EverythingConverter

config = {"merge_slashes": False}

@app.route('/<everything:ctx>', methods=['GET', 'POST'], **config)
def main(ctx=""):
    return "hello world"

# gunicorn --workers=2 'app:create_app()' --bind=0.0.0.0:<port>
def create_app():
    return app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000)
    
    #test 
    #with app.test_client() as c:
    #    rs = c.get("/")
    #    print(rs.data)
from flask import Flask, request, jsonify, json, abort, redirect, url_for, render_template
from flask_cors import CORS, cross_origin
import os
import re
import subprocess
import traceback

app = Flask(__name__, template_folder='template')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=['GET', 'POST'])
@cross_origin()
def main():
    return "hello world"

# gunicorn --workers=2 'app:create_app()' --bind=0.0.0.0:<port>
def create_app():
    return app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000)
    
    #test 
    #with app.test_client() as c:
    #    rs = c.get("/")
    #    print(rs.data)

Query params to dict

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/")
def hello():
    all_args = request.args.to_dict()
    return jsonify(all_args)

https://stackoverflow.com/questions/24292766/how-to-process-get-query-string-with-flask

Post to dict

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/", methods=["POST"])
def hello():
    req = request.json
    return jsonify(all_args)

https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask

Upload file

from flask import Flask, request, jsonify, json, abort, redirect, url_for, render_template
from flask_cors import CORS, cross_origin
import os
import re
import subprocess
import traceback

app = Flask(__name__, template_folder='template')
cors = CORS(app)

@app.route('/', methods=['GET', 'POST'])
def main():
    if request.method == 'GET':
        return """
        hello world
        <form enctype="multipart/form-data" method="post">
        <input type="file" name="file" onchange="form.submit()"/><br>
        <button>upload</button>
        </form>
        """
    if request.method == 'POST':
        for f in request.files.getlist('file'):
            #remote_path = request.form.get('remote_path', 'default.mp4')
            print("file", f, f.filename)
            f.save(f.filename)
            #f.save(remote_path)
    return redirect("/")

# gunicorn --workers=2 'app:create_app()' --bind=0.0.0.0:<port>
def create_app():
    return app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

    #test
    #with app.test_client() as c:
    #    rs = c.get("/")
    #    print(rs.data)