i am trying to run my app.py which is in flask with "python app.py" instead of "flask run". i have the following in the app.py. but when i run it via python, the html pages dont show up and says requested url not found. i am not sure what i am missing here.
keep in mind the app runs fine when i use flask run command. the webpage shows on the browser fine. but when i use the "python app.py" the browser says the url is not found. i am not sure if i need to redirect something or not
app = Flask(__name__)
if __name__ == "__main__":
app.run(host="10.147.180.15", port="80", debug=True)
#app.route('/')
def index():
return render_template("index.html")
any help will be greatly appreciate it
You should declare app before initiating routes:
app = Flask(__name__)
#app.route('/')
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
Edited: fixed indentation
Related
In Spyder, I wrote this code.
Why is it not showing on my browser localhost:5000?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Instead of app.run() use app.run(debug = False), for the purpose of running.
How are you running your script? Your code SHOULD work. You have a couple of options:
Navigate to the folder your script is in within a terminal/cmd and enter the following:
python3 script.py
replacing script.py with the actual name of your script
Alternatively:
python3 script.py
If it successfully boots up the flask server it will give you the address and port its running on. By default it should be port 5000 like you have said.
But the address could be:
http://127.0.0.1:5000
localhost:5000
So try both, essentially the same but your computer might be weird.
I'm not familiar with the Spyder IDE, if it has a run button to start the script then press that and you should be able to access the server via either of the above addresses.
You could also try specifying a new port, maybe 5000 is being used?
app.run(port=8080)
Try specifying the host and port like this:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)
I'm attempting to deploy a simple web app, and I'm using command line waitress-serve --call command. But every time, the command immediately returns 1. Malformed application 'name_of_project_here'.
Here's my flask web app in python:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run(debug = True)
and the command I run is just
waitress-serve --call "name_of_project"
I did try looking through the documentation, and I found where the error occurs, but couldn't find an explanation of why it's occurring. What does malformed application mean?
Minimal working example:
If you put code in file main.py
from flask import Flask
def create_app():
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
return app
if __name__ == '__main__':
app = create_app()
app.run()
then you can run it as
waitress-serve --call "main:create_app"
So "name_of_project" has to be "filename:function_name" which creates Flask() instance.
It can't be any text. And if you forget : then you may see "Malformed application"
when defining your appname and the function that initialize your app with : dont put them into quotes('')
I am trying to host a flask server from my windows computer so I can access it from external devices
I am using Flask/Python and have already tried a few things but can't get it to work
Tried running it on 0.0.0.0, port 33, 5000, etc. but I still can't access it this way
from flask import Flask, request, abort
app = Flask(__name__)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=33)
When I then run the file I get:
Running on http://0.0.0.0:33/ (Press CTRL+C to quit)
But it isn't even running there, nor on any other way I can access it
I expect to be able to access my flask application and send requests to it by using my public IP address
What can I do here to make it work?
You have missed an important line in your code:
After the line
app = Flask(__name__)
You have to write the line:
#app.route('/')
We use the route() decorator to tell Flask what URL should trigger our function.
And then define a function that will tell what task to be performed in the web app hosted in the respective address.
The function might look something like this:
def hello_world():
return 'Hello, World!'
The complete code then will look like:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=33)
Hope this helps.
I trained a machine learning model which I wanted to deploy as an app. I learnt that flask is especially suited for this.
I have two functions, get_data from the user of the web app and then, infer_results which prints the results of image type.
I am trying to set up flask for the above use case. I started by following this tutorial: https://sourcedexter.com/python-rest-api-flask/
What I did:
In [71]: app = Flask(__name__)
In [72]: #app.route("/me", methods=["GET"])
...: def get_results():
...: return "Dummy Result"
And then,
In [73]: app.run(host="0.0.0.0", threaded=True)
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
But all I get is:
I can't figure out what is going wrong? This is my main question.
On a sidenote, it would be really great if you could offer some advice or suggestion on : how should I go about building my app: I want to do in Python only. But then, how should I design the UI: where user can put his data and upload it? Is there a way to package all my code (the machine learning code + the user input/output) in a desktop application that user can download and run on his PC?
Instead of app.run(host="0.0.0.0", threaded=True)
Use:
app.run(host="localhost", threaded=True)
Or Execute the below script:
from flask import Flask, render_template
app = Flask(__name__)
# index
#app.route('/')
def index():
return "Hello"
# /me
#app.route("/me", methods=["GET"])
def get_results():
return "Dummy Result"
if __name__ == "__main__":
app.run()
Consider the following minimal working flask app:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "I am /"
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
This happily works. But when I try to make a GET request with the "requests" module from the hello route to the api route - I never get a response in the browser when trying to access http://127.0.0.1:5000/
from flask import Flask
import requests
app = Flask(__name__)
#app.route("/")
def hello():
r = requests.get("http://127.0.0.1:5000/api")
return "I am /" # This never happens :(
#app.route("/api")
def api():
return "I am /api"
if __name__ == "__main__":
app.run()
So my questions are: Why does this happen and how can I fix this?
You are running your WSGI app with the Flask test server, which by default uses a single thread to handle requests. So when your one request thread tries to call back into the same server, it is still busy trying to handle that one request.
You'll need to enable threading:
if __name__ == "__main__":
app.run(threaded=True)
or use a more advanced WSGI server; see Deployment Options.