This question already has answers here:
Display the contents of a log file as it is updated
(3 answers)
Closed 3 years ago.
I have script which executes for 10 minutes and generates logs/outputs while executing..i want these logs to be passed to html file using flask.
You can use ajax methods in your javascript code to ask server for new logs/outputs with any period.
You can open WebSocket and write your logs to client in real-time. Look at example on flask-socketIO documentation: https://flask-socketio.readthedocs.io/en/latest/ or somewhere else.
Here is example how to send message to client:
from flask import Flask, render_template
from flask_socketio import SocketIO, send
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
#socketio.on('message')
def handle_message(message):
send(message)
if __name__ == '__main__':
socketio.run(app)
Related
This question already has answers here:
How to run a flask application?
(6 answers)
Closed 2 years ago.
I am following the flask quickstart to create a python server so that I can send data from postman to the server. However, when I run the code using "python main.py", nothing happens in the command line. What am I doing wrong?
Code:
from flask import Flask, request
from flask_restful import Resource, Api
from server import analyzeWeaknesses
app = Flask(__name__)
api = Api(app)
#app.route('/analyzeWeaknesses', methods=['POST'])
def WeaknessAnalysis():
if request.method == 'POST':
analyzeWeaknesses(request.data)
return {"status":"success"}
else:
error = 'Unable to access data!'
analyzeWeaknesses simply takes in an input and does an analysis of it and outputs integers. I would like this output to be also returned to postman. Desired outcome Postman to for python input and then receive its output
You're missing the main function. Add this at the bottom of your code-
if __name__ == '__main__':
app.run()
This question already has answers here:
How to run a flask application?
(6 answers)
Configure Flask dev server to be visible across the network
(17 answers)
Closed 2 years ago.
I'm trying to run my first flask site, but it just brings up the "This site can't be reached" message. What am I doing wrong?
Code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def welcome():
return f"<h1>Welcome!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)
Replace host='0.0.0.0' with your IP Address or you can just remove it.
Just return localhost for your development needs, even it comes to production it's okay because you could connect the app with Nginx/Apache.
This question already has answers here:
Auto reloading python Flask app upon code changes
(14 answers)
Closed 5 years ago.
At the first time when I'm running the below code in Python, it's succesfully running in localhost by displaying hello, I'm using the atom editor.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_wrld():
return "hello"
if __name__ == '__main__':
app.run()
But when I'm changing my return to hello python as follows:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def dhineTrend():
return "hello python"
if __name__ == '__main__':
app.run()
It's running in localhost but when I'm hitting the browser it keep on showing old hello. How to overcome it?
Note: After first run I cut the run, then only request the second.
Enable DEBUG to auto reload files on change,
app = Flask(__name__)
app.debug = True
There are multiple ways to do this,
app.config.update(DEBUG=True)
Or
app.config['DEBUG'] = True
Or, Create a config.py defining all the Flask settings
app.config.from_object('config')
This question already has an answer here:
Flask view raises TypeError: 'bool' object is not callable
(1 answer)
Closed 6 years ago.
I am trying to transition my workflow into Flask to write a simple web interface for a Python script.
However, doing the following, raises a type error constantly:
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
#app.route('/restart/<int:id>')
def restart(id):
return id
if __name__ == '__main__':
app.run()
I would basically just like to show the id that is passed in the URL.
Am I missing something? This is exactly how I would do this in Django for example and all the examples on the Net have pointed to this approach in Flask.
Your route function should be returning a string but you're returning the integer you're passing into it. Cast it to a string instead:
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
#app.route('/restart/<int:id>')
def restart(id):
return str(id)
if __name__ == '__main__':
app.run()
This question already has answers here:
RuntimeError: working outside of application context
(4 answers)
Closed 6 years ago.
I have a project which is structured similar to the overholt and fbone example. I can send emails from all my blueprints fine, but struggle to send outside. E.g. from within a cron script or celery task.
I keep getting the error working outside of application context
app/factory.py
from flask import Flask
from .extensions import mail
def create_app(package_name, package_path, settings_override=None,
register_security_blueprint=True):
app = Flask(package_name, instance_relative_config=True)
mail.init_app(app)
register_blueprints(app, package_name, package_path)
app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)
return app
app/extensions.py
from flask_mail import Mail
mail = Mail()
app/frontend/admin.py
bp = Blueprint('admin', __name__, url_prefix='/admin', static_folder='static')
#bp.route('/')
def admin():
msg = Message(......)
mail.send(msg)
Everything up until here works fine. Now I have a separate module in app which has some cron scripts which now fail.
app/cron/alerts.py
from ..extensions import mail
from flask.ext.mail import Message
def alert():
msg = Message('asdfasdf', sender='hello#example.com', recipients=['hello#example.com'])
msg.body = 'hello'
mail.send(msg)
Which produces the error. How can I get around this?
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
You need use Flask-Mail:
from flask_mail import Mail
mail = Mail(app)
I would recommend going with celery. For the context problem I have found my solution in the following.
Have a look at this:
Miguel Grinberg's Blogpost on celery
Or if you are using the factory pattern in your application:
Celery with Factory Pattern
The second one is some kind of further/extended reading.
Both of them helped me a lot. (The second one also solved a context issue for me)