For the last few weeks I've been building a website mostly based on Miguel Grinberg's book 'Flask Web Development'. This is my 'manage.py' file for reference:
import os
from app import create_app, db
from app.models import User, Role, Permission, Post
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
Permission=Permission, Post=Post)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
And this is my app/__init.py file:
from flask import Flask #session, flash, url_for
from flask.ext.mail import Mail
from flask.ext.moment import Moment
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
from config import config
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'
mail = Mail()
moment = Moment()
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
login_manager.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
from main import main as main_blueprint
from .auth import auth as auth_blueprint
app.register_blueprint(main_blueprint)
app.register_blueprint(auth_blueprint, url_prefix = '/auth')
return app
As you can see it's a relatively bare-bones project, but it was going well until a few days ago when I accidentally deleted my migrations folder, which up until that point was working without perfectly fine. When I attempted to set up the migrations folder again, I got this error:
(website1)joshua#joshua-ThinkPad-Edge-E430 ~/website/website1 $ python manage.py db migrate
Traceback (most recent call last):
File "manage.py", line 29, in <module>
manager.run()
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_migrate/__init__.py", line 153, in migrate
config = _get_config(directory, opts=['autogenerate'])
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/flask_migrate/__init__.py", line 51, in _get_config
config.set_main_option('script_location', directory)
File "/home/joshua/.virtualenvs/website1/lib/python2.7/site-packages/alembic/config.py", line 201, in set_main_option
self.file_config.set(self.config_ini_section, name, value)
File "/home/joshua/anaconda/lib/python2.7/ConfigParser.py", line 753, in set
ConfigParser.set(self, section, option, value)
File "/home/joshua/anaconda/lib/python2.7/ConfigParser.py", line 396, in set
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'alembic'
I'm not really sure what to do about this, I've tried dropping then re-creating my database and running db migrate again but I got the same error as before. I also tried uninstalling and reinstalling alembic but with no luck.
I'm not really sure what do do at this point, up until now whenever I've had an issue with the tutorial I took the time to figure it out on my own since I want to understand how everything works, but I'm totally stumped on this one.
Courtesy Dauros: I neglected to run python manage.py db init.
Related
I'm trying to use debug mode in Flask, but it's crashing every time. Ny config.py file looks like this:
class Config(object):
DEBUG = False
DEVELOPMENT = False
SECRET_KEY = 'do-i-really-need-this'
FLASK_SECRET = SECRET_KEY
SQLALCHEMY_DATABASE_URI = 'host'
class DevConfig(Config):
DEVELOPMENT = True
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'host2'
Then, init.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app():
"""Construct the core application."""
app = Flask(__name__, instance_relative_config=False)
app.config.from_object('config.DevConfig')
db.init_app(app)
with app.app_context():
# Imports
from . import routes
# Create tables for our models
db.create_all()
return app
And finally, the run file:
from application import create_app
app = create_app()
if __name__ == "__main__":
app.run(host='0.0.0.0')
When I try to run it, following error appears:
* Serving Flask app "application" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
INFO:werkzeug: * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
Traceback (most recent call last):
File "<ipython-input-1-aaa9539d5209>", line 1, in <module>
runfile('/home/vladimir.balayan/Desktop/scipts/UX/flasksqlalchemy-tutorial-master/wsgi.py', wdir='/home/vladimir.balayan/Desktop/scipts/UX/flasksqlalchemy-tutorial-master')
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/vladimir.balayan/Desktop/scipts/UX/flasksqlalchemy-tutorial-master/wsgi.py", line 11, in <module>
app.run(host='0.0.0.0')
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/flask/app.py", line 990, in run
run_simple(host, port, self, **options)
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/werkzeug/serving.py", line 1007, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/werkzeug/_reloader.py", line 159, in restart_with_reloader
args = _get_args_for_reloading()
File "/home/vladimir.balayan/anaconda3/envs/wbe-testing/lib/python3.7/site-packages/werkzeug/_reloader.py", line 76, in _get_args_for_reloading
if __main__.__package__ is None:
AttributeError: module '__main__' has no attribute '__package__'
I already installed python-dotenv module and made .env file with FLASK_ENV=development, but the error is the same.
I'm using Spyder IDE for debugging and this tutorial
If your application structure is the same as shown in the tutorial you are following, try changing your config.py to this
from os import environ, path
basedir = path.abspath(path.dirname(__file__))
class Config(object):
DEBUG = False
DEVELOPMENT = False
SECRET_KEY = environ.get('SECRET_KEY') or \
'\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'
FLASK_SECRET = SECRET_KEY
class DevConfig(Config):
DEVELOPMENT = True
DEBUG = True
SQLALCHEMY_DATABASE_URI = environ.get('SQLALCHEMY_DATABASE_URI') or \
'sqlite:///' + os.path.join(basedir, 'db.sqlite')
class ProdConfig(Config):
DEVELOPMENT = True
DEBUG = True
SQLALCHEMY_DATABASE_URI = environ.get('SQLALCHEMY_DATABASE_URI') or \
'sqlite:///' + os.path.join(basedir, 'db.sqlite')
config = {
'dev': DevConfig,
'prod': ProdConfig,
'default': DevConfig,
}
And then your __init__.py to this
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
db = SQLAlchemy()
def create_app(config_name):
"""Construct the core application."""
app = Flask(__name__)
app.config.from_object(config.get(config_name or 'default'))
db.init_app(app)
with app.app_context():
# Imports
from . import routes
# Create tables for our models
db.create_all()
return app
Then in your run.py or whatever file you are using to launch the application you could do this
from os import environ
from application import create_app
app = create_app(environ.get('FLASK_CONFIG'))
if __name__ == "__main__":
app.run(host='0.0.0.0')
Hope this helps.
I'm debugging an error, when I try to query from my database to populate the fields of my FlaskForm, I get the following error:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
app = create_app(os.getenv('DVR_CONFIG') or 'default')
File "C:\Users\---\myapp\\app\__init__.py", line 42, in create_app
from .main import main as main_blueprint
File "C:\Users\---\myapp\\app\main\__init__.py", line 5, in <module>
from . import views, errors
File "C:\Users\---\myapp\\app\main\views.py", line 5, in <module>
from .forms import CharacterSelect
File "C:\Users\---\myapp\\app\main\forms.py", line 15, in <module>
class CharacterSelect(FlaskForm):
File "C:\Users\---\myapp\\app\main\forms.py", line 17, in CharacterSelect
user = User.query.filter_by(unique_name=session.get('unique_name')).first()
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 500, in __get__
return type.query_class(mapper, session=self.sa.session())
File "C:\Python27\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in __call__
return self.registry()
File "C:\Python27\lib\site-packages\sqlalchemy\util\_collections.py", line 990, in __call__
return self.registry.setdefault(key, self.createfunc())
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 771, in create_session
return SignallingSession(self, **options)
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 153, in __init__
self.app = app = db.get_app()
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 922, in get_app
raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
Here is the offending code:
from ..models import User
class CharacterSelect(FlaskForm):
user = User.query.filter_by(unique_name=session.get('unique_name')).first()
charId0 = user.characterId_0
charId1 = user.characterId_1
charId2 = user.characterId_2
user_list = [charId0, 'Char0', charId1, 'Char1', charId2, 'Char2']
new_user_list = [x for x in user_list if x is not None]
selectedUser = SelectField(u'Character Select', choices=user_list)
From researching this error, it appears Flask doesn't know which app my DB is attached to. Also most of the stackoverflow answers I have seen recommend using:
with app.app_context():
:
:
db.create_all()
However, from working through Miguels Flask Book and Flasky blog, he doesn't seem to need to use "with app.app_context()" and only uses "db.create_all()" in his unit test code. Miguel inits the DB like so:
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
:
:
db.init_app(app)
Any help in debugging and understanding this error is much appreciated!
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
db.init_app(app)
return app
Many things are nor clear from your code snippet. What is obvious however is that you have to have your db.init_app(app) inside your create_app() function. Then you would return app object from the function. Most preferably in different module i.e. manage.py (this really depends on your application structure)
from somewhere import create_app
app = create_app(config_name)
if __name__ == "__main__":
app.run()
Now variable app is your application instance with db registered.
Someone has pointed me to the FlaskForm Docs, where the is a pretty sparse example.
Esentially I need to create the tuple list in my views.py and pass it into my Form Object, see below for details:
views.py:
user = g.user
charId0 = user.characterId_0
charId1 = user.characterId_1
charId2 = user.characterId_2
user_list = [(charId0, 'Char0'), (charId1, 'Char1'), (charId2, 'Char2')]
form = CharacterSelect(request.form)
form.selectedUser.choices = user_list
forms.py:
class CharacterSelect(FlaskForm):
selectedUser = SelectField(u'Character Select', choices=[])
I've been trying to make the code on https://github.com/graup/flask-restless-security work for a while. After I had failed to incorporate this in my own code I decided to simply just use this prepared code. However when I try to run server.py it raises the error below. Origin of the error is apimanager of flask-restless. I opened an issue on git however apparently the project is not maintained anymore. How can I make this work?
Traceback (most recent call last):
File "E:/WEB/Project1/app/server.py", line 62, in
include_columns=['id', 'data1', 'data2', 'user_id'])
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\manager.py", line 698, in create_api
blueprint = self.create_api_blueprint(app=app, *args, **kw)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\manager.py", line 566, in create_api_blueprint
pk_name = primary_key or primary_key_name(model)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\helpers.py", line 232, in primary_key_name
pk_names = primary_key_names(model)
File "E:\WEB\Project1\flask\lib\site-packages\flask_restless\helpers.py", line 214, in primary_key_names
return [key for key, field in inspect.getmembers(model)
File "c:\python27\Lib\inspect.py", line 252, in getmembers
value = getattr(object, key)
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 498, in get
return type.query_class(mapper, session=self.sa.session())
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in call
return self.registry()
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\util_collections.py", line 990, in call
return self.registry.setdefault(key, self.createfunc())
File "E:\WEB\Project1\flask\lib\site-packages\sqlalchemy\orm\session.py", line 2829, in call
return self.class_(**local_kw)
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 143, in init
self.app = app = db.get_app()
File "E:\WEB\Project1\flask\lib\site-packages\flask_sqlalchemy_init_.py", line 957, in get_app
'application not registered on db instance and no application'
RuntimeError: application not registered on db instance and no applicationbound to current context
server.py
from database import db
from application import app
from flask_restless import APIManager
from models import SomeStuff
apimanager = APIManager(app, flask_sqlalchemy_db=db)
apimanager.create_api(SomeStuff,
methods=['GET', 'POST', 'DELETE', 'PUT'],
url_prefix='/api/v1',
collection_name='free_stuff',
include_columns=['id', 'data1', 'data2', 'user_id'])
application.py
from flask import Flask
app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
database.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
models.py
from database import db
from flask_security import UserMixin, RoleMixin, SQLAlchemyUserDatastore
class SomeStuff(db.Model):
__tablename__ = 'somestuff'
id = db.Column(db.Integer, primary_key=True)
data1 = db.Column(db.Integer)
data2 = db.Column(db.String(10))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
user = db.relationship(User, lazy='joined', join_depth=1, viewonly=True)
The error you have now:
flask_sqlalchemy_init_.py
RuntimeError: application not registered on db instance and no
applicationbound to current context
is at flask_sqlalchemy usage, that not define a app instance.
The solution is register the application on db instance.
from flask_sqlalchemy import SQLAlchemy
from application import app
db = SQLAlchemy(app)
If I import logstash, running locally I get the following error
Connected to pydev debugger (build 162.1812.1)
/home/vagrant/.envs/emailservice/lib/python3.4/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.cache is deprecated, use flask_cache instead.
.format(x=modname), ExtDeprecationWarning
Traceback (most recent call last):
File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/home/vagrant/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/emailService/app.py", line 59, in <module>
import logstash
File "/home/vagrant/.envs/emailservice/lib/python3.4/site-packages/logstash/__init__.py", line 2, in <module>
from event import Event
ImportError: No module named 'event'
Process finished with exit code 1
my app.py file looks mostly like this. I run it locally through a vagrant session. If I remove the import logstash from the local branch of the if, the application starts up fine and I get local console log output.
import logging
import os
import sys
from flask import Flask
from flask_restful import Api
from flask_cache import Cache
from flask_sqlalchemy import SQLAlchemy
from opbeat.contrib.flask import Opbeat
from tasks import make_celery
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'SUCHSECRETSWOW')
app.config.from_object(os.environ.get('APP_SETTINGS', 'config.DevelopmentConfig'))
cache = Cache(app)
db = SQLAlchemy(app)
api = Api(app)
celery = make_celery(app)
if len(app.config['OPBEAT_ORGANIZATION_ID']):
opbeat = Opbeat(
app,
organization_id=app.config['OPBEAT_ORGANIZATION_ID'],
app_id=app.config['OPBEAT_APP_ID'],
secret_token=app.config['OPBEAT_SECRET_TOKEN'],
)
#app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
def clear_cache():
cache.clear()
def start_resources():
from emailService.api import HealthApi
api.add_resource(HealthApi, '/health')
def start_tasks():
from emailService.tasks import KickoffFetchEmailPeriodTask
if __name__ == '__main__':
if app.config.get('DEVELOPMENT', False):
#The reason this exists is purely because of my error.
import logstash
app.logger.setLevel(logging.DEBUG)
app.logger.addHandler(logging.StreamHandler())
else:
import logstash
app.logger = logging.getLogger('python-logstash-logger')
app.logger.setLevel(logging.INFO)
app.logger.addHandler(logstash.LogstashHandler('myhost.veryhost.suchhost', 5959, version=1))
app.logger.addHandler(logging.StreamHandler())
clear_cache()
start_tasks()
start_resources()
app.logger.debug('Starting app')
app.run(host='0.0.0.0', port=16600, debug=True, use_reloader=False)
All of the google searches result in a great big fat sum total of nothing helpful.
You're probably running into this issue, you have pip installed logstash instead of python-logstash
Run this and it should work afterwards:
> pip uninstall logstash
> pip install python-logstash
I am using Flask, Celery, Redis and Mysql to build website. Now I am facing one problem. I want to send every SQL query to celery and once job is done I then get query result from celery.result.
Here is part of my code:
Manage.py
import os
from app import create_app, db
from app.models import Users, Items
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
app.app_context().push()
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, Users=Users, Items=Items)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
query.py
from .models import Items, Users
from sqlalchemy import desc
from app import create_celery_app
celery = create_celery_app()
#celery.task
def user_query(id):
return Users.query.filter_by(id = id).first()
view.py
from ..query import user_query
...
userresult = user_query.delay(1)
main.py
from flask import Blueprint
main = Blueprint('main', __name__)
from . import views, errors
app.py
from flask import Flask
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from config import config, Config
from celery import Celery
from flask.ext.sqlalchemy import SQLAlchemy
import os
bootstrap = Bootstrap()
moment = Moment()
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
moment.init_app(app)
db.init_app(app)
from main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
def create_celery_app(app=None):
app = app or create_app(os.getenv('FLASK_CONFIG') or 'default')
celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'], include=['app.query'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
When I run celery worker I get error:
File "/home/yx/Documents/flasky/venv/bin/celery", line 11, in <module>
sys.exit(main())
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/__main__.py", line 30, in main
main()
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 770, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 309, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
self.app = self.find_app(app)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 489, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/app/utils.py", line 229, in find_app
sym = symbol_by_name(app, imp=imp)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/bin/base.py", line 492, in symbol_by_name
return symbol_by_name(name, imp=imp)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/kombu/utils/__init__.py", line 96, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/home/yx/Documents/flasky/venv/local/lib/python2.7/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/yx/Documents/flasky/app/query.py", line 6, in <module>
celery = create_celery_app()
File "/home/yx/Documents/flasky/app/__init__.py", line 29, in create_celery_app
app = app or create_app(os.getenv('FLASK_CONFIG') or 'default')
File "/home/yx/Documents/flasky/app/__init__.py", line 24, in create_app
from main import main as main_blueprint
File "/home/yx/Documents/flasky/app/main/__init__.py", line 5, in <module>
from . import views, errors
File "/home/yx/Documents/flasky/app/main/views.py", line 7, in <module>
from ..query import user_query
ImportError: cannot import name user_query
But user_query is from query.py, so does anyone know if I miss something?
Thank you.