Beginner Flask Site Issue [duplicate] - python

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.

Related

unable to access flask with postman [duplicate]

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()

How to pass data from flask to html file continuously? [duplicate]

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)

App initializes twice. Is this typical behavior? [duplicate]

This question already has answers here:
Why does running the Flask dev server run itself twice?
(7 answers)
Closed 4 years ago.
I'm currently testing a socket io app and I'm noticing unusual behavior.
In my init python script I'm declaring a function that instantiates a flask object and initializes it via a socketIO instance (which is global) i.e. (assuming all of these files are within the same directory scope):
#/test_app/configs/__init__.py
__socketIO__ = SocketIO()
def create_app(address, port):
app = Flask(__name__, static_url_path='')
app.config.from_pyfile('config.py')
__socketIO__.init_app(app)
return app
in another python script:
#/test_app/run_app.py
from configs import create_app
from configs import __socketIO__ as launch_socket
ip_address = '0, 0, 0, 0'
port = 5000
APP = create_app(ip_address, port)
if __name__ == '__main__':
print 'launching....'
launch_socket.run(APP, debug=True, host=ip_addres, port=port)
Given this, if I run run_app.py, 'launching...' will print twice.
Is this behavior typical? Granted I'm still learning my way around socket.io so perhaps I'm missing some point to it. It just seems odd that it initializes twice. Is there a way to circumvent this behavior if it's unnecessary or detrimental?
Thanks!
This is normal when you run in debug mode. Flask launches two processes, one for watching the code for changes and one for the actual flask process.

error tracking for server on flask [duplicate]

This question already has answers here:
Flask application traceback doesn't show up in server log
(4 answers)
Closed 5 years ago.
I have a server written on flask and I need to do error tracking (name of error, what caused the error, name of file and line with error).
How should I do it?
I'll appreciate any help.
enable the flask debugger, by setting debug to True for your flask app.
app = Flask(__name__)
app.config['DEBUG'] = True
As you shouldn't enable debug for a production server, you can create a logging
handler as follows: http://flask.pocoo.org/docs/0.12/errorhandling/

python local host http://127.0.0.1:5000/ not refreshing to new request from flask [duplicate]

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')

Categories