Python Forum

Full Version: Flask tutorial:
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to get the Flask tutorial running. However I have ran into a problem of an undefined method used in the tutorial.

$ flask init-db   
Traceback (most recent call last):
  File "/home/john/devel/Flask/flask-tutorial/venv/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/flask/cli.py", line 412, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/john/devel/Flask/flask-tutorial/flaskr/db.py", line 21, in init_db_command
    init_db()
  File "/home/john/devel/Flask/flask-tutorial/flaskr/db.py", line 14, in init_db
    with current_app.open_resources('schema.sql') as f:
  File "/home/john/devel/Flask/flask-tutorial/venv/lib64/python3.7/site-packages/werkzeug/local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Flask' object has no attribute 'open_resources'
flaskr/__init__.py:
import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    from . import db
    db.init_app(app)

    return app
flaskr/db.py
import sqlite3

import click
from flask import current_app, g
from flask.cli import with_appcontext

def init_app(app):
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

def init_db():
    db = get_db()

    with current_app.open_resources('schema.sql') as f:
        db.executescript(f.read().decode('utf8'))

@click.command('init-db')
@with_appcontext
def init_db_command():
    """Clear the existing data and create new tables."""
    init_db()
    click.echo('Initialized the database.')

def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db


def close_db(e=None):
    db = g.pop('db', None)

    if db is not None:
        db.close()

There is a typo in the tutorial and it is supposed to just be open_resource
Is this the tutorial you followed? I don't see the typo...
http://flask.pocoo.org/docs/1.0/tutorial/database/#id3 Wrote:
def init_db():
    db = get_db()

    with current_app.open_resource('schema.sql') as f:
        db.executescript(f.read().decode('utf8'))


@click.command('init-db')
@with_appcontext
def init_db_command():
    """Clear the existing data and create new tables."""
    init_db()
    click.echo('Initialized the database.')