Running Flask app.py works from Terminal, but not from PyCharm - python

I am trying to get the hang of Flask. I specifically want to learn using Flask with PyCharm.
I have set up the minimal example from the Flask website. When I run it from the terminal with flask run it works, which means I can open up the website.
When I run from PyCharm, it also seems to work, but I get an 404 error when I try to open up the site.
Am I missing some configuration in PyCharm?
Here's the app.py file:
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run(port=5000)
#app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

I believe in Pycharm everything runs in a virtual environment. That is why you cannot access your site. Try going to http://localhost:5000 while the app is running in Pycharm. If that doesn't work try it from the terminal. If it works in the terminal then come back to this answer and reply that it doesn't work.

Related

How do I run my Flask app if nothing happens when i run it through powershell? [duplicate]

This question already has answers here:
How to run a flask application?
(6 answers)
Closed 1 year ago.
I have just started learning about Flask. I installed it on a virtual env.
My app.py file looks like this.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World"
if __name__ == '__main__':
app.run(debug=True)
when i run python app.py in powershell nothing happens and it does nothing. I checked that the python is installed and flask is also installed.
It's hard to give you an answer because we do not have access to your computer.
There may be files or programs interfering with your application.
I would recommend reading this blog post about running a Flask application.
Alternatively, you could debug your program using Visual Studio Code or another IDE.
Try finding the port your program is running on and see if you are viewing that port. Read this similar question for help. For example, try using app.run(port=4996).

Succeeded in Deploying Flask app at Azure, but app does not work upon opening website

I have a problem with deploying my flask application to azure. I am currently deploying it through github, and deployed successfully. But upon opening the website, it gives me errors. Mainly this (looked on Application logs):
2019-11-15T02:22:03.098959319Z /opt/startup/startup.sh: 23:
/opt/startup/startup.sh: gunicorn --bind=0.0.0.0 --timeout: not found
and this:
Container goodnewsviagithub_0_a04a1362 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
My entry point is gunicorn --bind=0.0.0.0 --timeout 600 run:app
: And my code is
Is this because of my flask structure?
Any help would be great! I am really stuck on this.
Thank you very much again! Hope you all have a great day.
Soo it turns out to be the shebang in python that linux needs to identify the py file
#!/usr/bin/env python3
# ^ is the shebang, not a comment
from flaskapp import app
if __name__ == '__main__':
app.run(host='0.0.0.0',port="8400",debug=True)
#app.run(host='172.17.0.1',port="3306",debug=True)

Not able to reach Flask webserver

This is my test code running on Visual studio 2017, file name is test:
from flask import Flask, request #import main Flask class and request object
from test import app
app = Flask(__name__) #create the Flask app
#app.route('/query-example')
def query_example():
return 'Todo...'
#app.route('/form-example')
def formexample():
return 'Todo...'
#app.route('/json-example')
def jsonexample():
return 'Todo...'
if __name__ == '__main__':
app.run(debug=True, port=5000) #run app in debug mode on port 5000
But when I run it in Visual studio 2017 and I enter this route "http://127.0.0.1:5000/json-example" in Chrome Browser I always get this error message.
"404 Not Found"
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
I am a learner, tried following this article:
https://scotch.io/bar-talk/processing-incoming-request-data-in-flask
Python: 3.6
Flask: 0.12.4
pip: 18.1
Earlier I used to get the return message with the same code but after updating the flask to 0.12.4, its not working, I think something has changed. I am not able to debug the exact issue.
--Update--
Ok, after updating the flask version to 1.0.2, but still not able to reach the webserver from the url: http://127.0.0.1:5000/json-example. Please check out the screenshot below.
I get 404 error not found:
Not Found
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
The docs suggest that you should verify that .env & .flaskenv files are not interfering. (Likely they are not.)
shadowing
You really need to avoid shadowing one symbol with another.
Please don't name your module test, as python ships with a system library with that name. You might use test1 instead, to avoid the needless possibility of confusion.
Please rename your global variable to app_, since your module has an app.py file.
root URL
Please add a / slash route, even though it's not strictly necessary.
You will find that it aids debugging.
#app_.route('/')
def root():
return '<h1>top level</h1>'
version
Run a current version of flask, please.
It's much better to report problems with current rather than downrev code.
execution
Rather than having python directly call app_.run(...), please run flask instead. Use export or env, whichever you prefer:
$ export FLASK_APP=test1 FLASK_ENV=development
$ env FLASK_APP=test1 FLASK_ENV=development flask run --port=5000
This enables debug mode, which should help you get to the bottom of your routing issues.
Plus, reloading after edits is quite convenient.
The key is that rather than running python, you run flask which in turn runs python.
While I was troubleshooting, I noticed that the above same code works in raspberry Pi zero module with same python and flask version. But it is still not working in visual studio on windows 7. Probably there might be some issue with my visual studio. I will uninstall it reinstall it again. But yes my issue is solved for now. Thanks everyone for answering.
I had the same issue running VS Code on Ubuntu 19.10. The problem was returned string from route view function.
#app.route('/')
#app.route('/index')
def index():
return 'Hello World!'
gives error 404. But when I changed to :
#app.route('/')
#app.route('/index')
def index():
return '<h1>Hello World!</h1>'
everything was just fine.

How to change and reload python code in waitress without restarting the server?

I am using waitress to serve the web application content like.
waitress-serve --port=8000 myapp:application
While developing, as I change code, I continuously had to restart the waitress-serve to see my changes. Is there a standard way I can automate this?
I know this is an old question but I had a similar issue trying to enable hot reload functionality for my REST API using Falcon framework.
Waitress does not monitor file changes but you could use hupper on top of it. Pretty simple:
$ pip install hupper
$ hupper -m waitress --port=8000 myapp:application
It works on Windows too.
Based on the comment by #Dirk, I found the archive.org link to the snippet mentioned. I was able to get Waitress reloading by using Werkseug directly. Using Werkzeug's decorator run_with_reloader causes the app to restart whenever a Python file changes. (Werkzeug is used within Flask so it should be available).
Additionally, the app.debug = True line causes Flask to reload template files when they change. So you may want both considering your particular situation.
import werkzeug.serving
#werkzeug.serving.run_with_reloader
def run_server():
app.debug = True
waitress.serve(app, listen='127.0.0.1:4432')
if __name__ == '__main__':
run_server()
Once I had my server set up this way, it auto-reloaded the server whenever any file changed.

Installing bottlepy and running python script

I am trying to run this simple python script through my terminal window:(it is the example on the bottlepy website)
from bottle import route, run, template
#route('/hello/:name')
def index(name='World'):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
In order to do so, I installed bottlepy from Terminal using
easy_install bottle
which instals it in /Library/Frameworks/Python.framework/Versions/4.2.30201/bin
When I try to run my python document in my terminal window:
cd ~/Dropbox/Work/MongoDB/
python hello.py
It returns:
ImportError: No module named bottle
I'm not sure what to do to put bottlepy in the correct emplacement so that it works. I'm sure it must be something silly but I can't think of what to do.
Thank you!!
xx
Take a look at the docs. You can download bottle.py directly to the folder with the rest of the progs, or install in the way shown.
In order to run the file mostly you need to create app first:
app = bottle.Bottle()
And later run it or serve by external web server like waitress

Categories

Resources