How can I run the full Flask Tutorial app in the Wingware IDE?
I've been using Flask under Wing Pro 7.2 for some time, and can get control because I start Flask by doing app.run() in Wing.
I conceived a wish to trace through the official working version of the completed tutorial, obtained by
git clone https://github.com/pallets/flask
This works fine (using 'flask run'), and I now have the complete source. But there's no app.run() anywhere. I tried putting one in init.py:
def create_app(test_config=None):
#...
db.init_app(app)
return app
RUN = True
if RUN:
app= create_app()
app.run()
and flask starts up, but throws an error on request 'localhost:5000/', which normally fires up a database form.
Is there a starting point in the Python code somewhere?
Or, is it possible to attach Wing to a running flask, and tell it about the source files? There is a bit in the Wing manual about attaching, but it seems to demand information about the target that we lack.
I managed to start the tutorial by creating a file main.py in the same directory as the flaskr package, with this contents:
import flaskr
app = flaskr.create_app()
app.debug = False
app.run(use_reloader=True)
Then I set this as the main debug file in Wing.
To make debugging work correctly, you may also need to set the Python Executable in Project Properties (from the Project menu) to the Python command line or activated env you want to use.
Also, it is important to set Debug/Execute > Debug Child Processes in Project Properties to Always Debug Child Processes. Otherwise the process actually running the app code is not debugged.
This works but results in a SQL error because the table 'post' does not exist if you did not already run the following first to initialize the database:
$ export FLASK_APP=flaskr
$ export FLASK_ENV=development
$ flask init-db
Once I did that, everything worked for me.
While I am running Flask code from my command line, a warning is appearing:
Serving Flask app "hello_flask" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
What does this mean?
As stated in the Flask documentation:
While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.
Given that a web application is expected to handle multiple concurrent requests from multiple users, Flask is warning you that the development server will not do this (by default). It recommends using a Web Server Gateway Interface (WSGI) server (numerous possibilities are listed in the deployment docs with further instructions for each) that will function as your web/application server and call Flask as it serves requests.
Try gevent:
from flask import Flask
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
#app.route('/api', methods=['GET'])
def index():
return "Hello, World!"
if __name__ == '__main__':
# Debug/Development
# app.run(debug=True, host="0.0.0.0", port="5000")
# Production
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
Note: Install gevent using pip install gevent
As of Flask 1.x, the default environment is set to production.
To use the development environment, create a file called .flaskenv and save it in the top-level (root) of your project directory. Set the FLASK_ENV=development in the .flaskenv file. You can also save the FLASK_APP=myapp.py.
Example:
myproject/.flaskenv:
FLASK_APP=myapp.py
FLASK_ENV=development
Then you just execute this on the command line:
flask run
That should take care of the warning.
To remove the "Do not use the development server in a production environment." warning, run:
export FLASK_ENV=development
before flask run.
I was typing flask run and then saw this message after that I solve this issue with these:
1- Add this text in your myproject/.flaskenv :
FLASK_APP=myapp.py
FLASK_ENV=development
also you should type "pip3 install python-dotenv" for using this file .flaskenv
2-in your project folder type in terminal your flask command which one you use :
flask-3 run
First, try to the following :
set FLASK_ENV=development
then run your app.
I have been using flask for quite some time now, and today, suddenly this warning turned up. I found this.
As mentioned here, as of flask version 1.0 the environment in which a flask app runs is by default set to production. If you run your app in an older flask version, you won't be seeing this warning.
New in version 1.0.
Changelog
The environment in which the Flask app runs is set by the FLASK_ENV environment variable. If not set it defaults to production. The other recognized environment is development. Flask and extensions may choose to enable behaviors based on the environment.
in configurations or config you can add this code :
ENV = ""
same as if you try to add debug set to true like this
DEBUG = True
for more detail you can check this http://flask.pocoo.org/docs/1.0/config/#ENV
It means the programe is run on production mode even in developing environment.so to avoid that warning, you need to define this is development environment.for that,Type and run below command in project directory on terminal(linux).
export FLASK_ENV=development
if you are windows user then run,
set FLASK_ENV=development
To disable the message I use:
app.env = "development"
You have to put this in the Python-Script before you run the app with:
app.run(host="localhost")
If you encounter NoAppException and you see lazy loading the following seemed to fix the issue:
cd <project directory>
export FLASK_APP=.
export FLASK_ENV=development
export FLASK_DEBUG=1
You can begin your main script like this :
import os
if __name__ == '__main__':
os.environ.setdefault('FLASK_ENV', 'development')
I've built a Django project that works, even after I freeze it using Cx_Freeze and Py2exe.
Now I'm trying to set up the project for distribution, which requires a real webserver. I'm going for Gunicorn (will add Nginx once it works). I managed to run the Gunicorn server properly through the command line using :
gunicorn wsgi:application
However, I need to be able to run the server from my Python script, as the server is ment to be localhost. Gunicorn used to be shipped with a command 'run_gunicorn' designed for Django, but this command is now deprecated.
I tried understanding the following method :
How to use Flask-Script and Gunicorn
But I can't figure out how to make it work with Django.
The following doesn't work:
from django.core.wsgi import get_wsgi_application
from gunicorn.app.base import Application
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = get_wsgi_application()
Application().run(application)
Could someone tell me how to start the gunicorn server from my Python script ?
It appeared that I successfully deployed the application using this code
import sys, os
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = "UR.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
However Django did not run properly because it could not get to the latest version of Django and ran an old version with Python 2.3. I installed the latest Django and Python 2.7 and I added those two lines and now I am getting an internal server error..
INTERP = '/home/username/opt/python-2.7.7/bin/python'
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
I am sure this is the right path to my Python 2.7 ... any help will be really appreciated
I finally solved it ,
I was deploying on dreamhost and their servers had django1.2 and python2.6, whenever i tried to make python2.7 and let it be the default passenger gets to python 2.6 and successfully executes python2.7 but for some reason it ran into django1.2 instead of the latest django i installed.
I installed python2.7 in /usr/ (using a sudoer account ofcourse) then i installed the new django into site packages and now I no longer need the lines that execute python2.7 as it is the default now along with django 1.7
I have also edited the passenger-wsgi file and this code worked very fine for me....
import sys, os
sys.path.append(os.path.join(os.getcwd(), 'projectname')) #I needed this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The only setback now is that the server is to slow in it's response but other than that everything went fine
And i found a great trick to get into the logs of django
ssh into your server
Execute python manage.py runserver
Open another ssh session at the same time
In the new ssh execute lynx localhost:8000
Then you will be able to see logs and debug information from django in the second ssh .
This does not work for me
$> cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
django 1.7rc3
celery 3.1.13
python 2.7
I attempt to run
celery worker -A <project_name>
and I get
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
The runserver command works fine so I don't think it has to do with my settings?
python manage.py runserver 0.0.0.0:8080
I've double checked celery.py and confirm it has the correct values for the following lines:
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
Is there something else i should be doing?
Django 1.7 now requires a different initialization for standalone scripts. When running outside the manage.py context, you now need to include:
import django
django.setup()
Try adding it before the app = Celery('proj') in your script.
I found out that the reason this was failing is because i had this in one of my tasks.py
CURRENT_DOMAIN = Site.objects.get_current().domain
I got side steped this for now with
CURRENT_DOMAIN = lambda: Site.objects.get_current().domain
currently waiting to see if anyone on github cares to offer a better recommendation.
https://github.com/celery/celery/issues/2227
will update if i get one. If not will probably just do a helper function that lazy returns the value i want.
update
at the suggestion of celery author, I've refactored my code so I don't make that call at the module level.
He's also addressed the issue by making sure django.setup() is called before importing task modules
https://github.com/celery/celery/issues/2227
I'm seeing the same issue however only with:
CELERYBEAT_SCHEDULER='djcelery.schedulers.DatabaseScheduler'
Do you have this enabled? If I run it with the default celery scheduler it loads fine. But can't get it to load with the Django scheduler.