Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using database from a library or script - how to? #20

Open
cleemesser opened this issue Mar 24, 2017 · 0 comments
Open

using database from a library or script - how to? #20

cleemesser opened this issue Mar 24, 2017 · 0 comments

Comments

@cleemesser
Copy link

cleemesser commented Mar 24, 2017

Hello,
I am new to flask, flask-sqlalchemy, and flask-diamond. My use case is that I would like to use flask-diamond to setup flask and sqlalchemy so that I can use my sqlalchemy database both as part of a web application and from my library and script codes outside of the web application.

In the flask-alchemy documentation, it sounds like one needs to initialize sqlAlchemy's init_app and then create an application context (http://flask-sqlalchemy.pocoo.org/2.1/contexts/). It is not clear to me how much of this is already being done for me in flask-diamond. Can you clarify?

Here is a template, assuming my flask-diamond application is myapplication. It appears to work, so I am guessing that I already have a context to work with.

Can you clarify?

Update

I've traced back through the Diamond code and it looks like, I had things wrong before. To set up to access my flask-diamond database from an outside library or script, I need to do:

# if flask-diamond has scaffolded out a new custom app called myapplication
# SETTINGS needs to be already set either in the environment (or via os.environ['SETTINGS'])
import myapplication
app = myapplication.create_app()

with app.app_context():
    do_stuff_with_app

If others agree this is correct, I can write this up as documentation if there is a place for recipies or how to information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant