Last active
July 26, 2020 19:59
-
-
Save VITIMan/042ef636c3dd626a0c9182fa4da29981 to your computer and use it in GitHub Desktop.
Flask snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download_file(): | |
""" | |
http://code.runnable.com/UiIdhKohv5JQAAB6/how-to-download-a-file-generated-on-the-fly-in-flask-for-python | |
""" | |
... | |
some_text = "" | |
filename = "FILENAME" | |
response = make_response(some_text, 200) | |
response.content_type = "text/plain" | |
response.headers[ | |
"Content-Disposition"] = "attachment; filename={}.txt".format( | |
filename) | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.reddit.com/r/learnpython/comments/282y58/how_to_change_terminal_output_format_in_flask/ | |
from flask import Flask | |
from werkzeug import serving | |
from werkzeug._internal import _log | |
class RequestLogHandler(serving.WSGIRequestHandler): | |
def log(self, type, message, *args): | |
# https://www.reddit.com/r/learnpython/comments/282y58/how_to_change_terminal_output_format_in_flask/ | |
# args = ('GET /client/O3454WOX/ HTTP/1.1', '200', '-') | |
_log(type, "address={}|method={}|path={}|status_code={}".format( | |
self.address_string(), self.command, self.path, args[1])) | |
app = flask.Flask(__name__) | |
if __name__ == "__main__": | |
app.run(debug = True, request_handler = LimitLogHandler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://stackoverflow.com/a/18969161 | |
################# blueprint.py | |
bp = Blueprint('burritos', __name__, | |
template_folder='templates') | |
@bp.route("/") | |
def index_page(): | |
return "This is a website about burritos" | |
@bp.route("/about") | |
def about_page(): | |
return "This is a website about burritos" | |
################ main.py | |
app = Flask(__name__) | |
app.register_blueprint(bp, url_prefix='/abc/123') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://stackoverflow.com/a/3760309 | |
request.remote_addr # Get the ip originator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment