Flask

Flask Restful add resource parameters

删除回忆录丶 提交于 2020-08-21 09:54:11
问题 I am looking to pass an object instance as a parameter into a Flask-RESTfull Resource. Here is my setup: # in main.py from flask import Flask from flask.ext.restful import Api from bar import Bar from foo import views app = Flask(__name__) api = Api(app) my_bar = Bar() api.add_resource(views.ApiPage, "/api/my/end/point/") Then in views.py I have the resource set up as follows: # In views.py from flask.ext.restful import Resource class ApiPage(Resource): def get(self): serialized = str(my_bar)

Logging stdout to gunicorn access log?

拈花ヽ惹草 提交于 2020-08-21 06:39:34
问题 When I wrap my Flask application in gunicorn writing to stdout no longer seems to go anywhere (simple print statements don't appear). Is there someway to either capture the stdout into the gunicorn access log, or get a handle to the access log and write to it directly? 回答1: Use the logging: set the stream to stdout import logging app.logger.addHandler(logging.StreamHandler(sys.stdout)) app.logger.setLevel(logging.DEBUG) app.logger.debug("Hello World") 回答2: The solution from John mee works,

Logging stdout to gunicorn access log?

北慕城南 提交于 2020-08-21 06:38:52
问题 When I wrap my Flask application in gunicorn writing to stdout no longer seems to go anywhere (simple print statements don't appear). Is there someway to either capture the stdout into the gunicorn access log, or get a handle to the access log and write to it directly? 回答1: Use the logging: set the stream to stdout import logging app.logger.addHandler(logging.StreamHandler(sys.stdout)) app.logger.setLevel(logging.DEBUG) app.logger.debug("Hello World") 回答2: The solution from John mee works,

Python Flask, how to detect a SSE client disconnect from front end Javascript

放肆的年华 提交于 2020-08-21 05:56:12
问题 Maybe this is a problem in Flask, there is no way to handle disconnection event on the server side. In Response class, there is a method named "call_on_close", where we can add a function without argument, e.g. on_close(), it will be fired when the response object's close method called, but this doesn't happen when I call EventSource.close() from the client side in Javascript. code on server side: from flask import Response r = Response(stream(), ...) r.call_on_close(on_close) return r def on

Flask-Session extension vs default session

风格不统一 提交于 2020-08-21 05:49:25
问题 I'm using: from flask import session @app.route('/') def main_page(): if session.get('key'): print ("session exist" + session.get('key')) else: print ("could not find session") session['key'] = '34544646###########' return render_template('index.html') I don't have the Flask-Session extension installed but this still works fine. I'm trying to understand why and when is that extension imp to me. As far as I see, the default session works well for me. 回答1: The difference is in where the session

Downloading dynamically generated files from a Dash/Flask app

巧了我就是萌 提交于 2020-08-21 05:35:42
问题 I tried to build a minimal example of a Dash app that illustrates the problem of dynamically generating a file that can then be downloaded via a download button. If you run this example, you will see a text area where text can be entered. A click on the "enter" button will store the text to a file and create a download button for the file. import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output, State import uuid

Excel export with Flask server and xlsxwriter

跟風遠走 提交于 2020-08-21 01:45:38
问题 So I've been using XLSXWriter in the past to export an excel file containing one tab filled with two pandas dataframes. In the past I've only been exporting the file to a local path on the user's computer but I'm doing the transition to a web interface. My desired output is to have the same excel file as the code below, but created in memory and sent to the user for him/her to download through the web interface. I've been seeing a lot of Django and StringIO but I'm looking for something that

Excel export with Flask server and xlsxwriter

独自空忆成欢 提交于 2020-08-21 01:43:28
问题 So I've been using XLSXWriter in the past to export an excel file containing one tab filled with two pandas dataframes. In the past I've only been exporting the file to a local path on the user's computer but I'm doing the transition to a web interface. My desired output is to have the same excel file as the code below, but created in memory and sent to the user for him/her to download through the web interface. I've been seeing a lot of Django and StringIO but I'm looking for something that

Python flask for loop doesn't print all of the outputs?

坚强是说给别人听的谎言 提交于 2020-08-20 16:04:09
问题 I have a dataset like this here: df= A B C house 50 50 money 80 50 bed 20 60 And this is my code: @app.route('/index', methods=['POST']) def index(): if request.method == 'POST' inpt = request.form['inpt'] for text in df['A']: if text == inpt: give_text = df[df['A'] == inpt] give_text = give_text.drop(['A'], axis =1) for column in give_text: column_edit = give_text[column] column_edit = column_edit * 5 return render_template('index.html', numbers = column_edit) And how would it change if this

How to serve static files in Flask

心不动则不痛 提交于 2020-08-20 14:58:23
问题 So this is embarrassing. I've got an application that I threw together in Flask and for now it is just serving up a single static HTML page with some links to CSS and JS. And I can't find where in the documentation Flask describes returning static files. Yes, I could use render_template but I know the data is not templatized. I'd have thought send_file or url_for was the right thing, but I could not get those to work. In the meantime, I am opening the files, reading content, and rigging up a