Last active
February 21, 2020 11:05
-
-
Save samukasmk/11f1ab0638abe8a680e40334e3399aa4 to your computer and use it in GitHub Desktop.
Pandas: read_sql from SQLAlchemy Models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Example of read_sql loading dataframe from database, using SQLAlchemy Models (possibily from flask app) | |
| # import pandas lib | |
| import pandas as pd | |
| # import sqlalchemy libs | |
| from flask_app.db import express_session | |
| from flask_app.models import SalesRegistry | |
| from flask_app.app import create_app, db | |
| # instantiate flask app to load db settings | |
| app = create_app() | |
| app.app_context().push() | |
| db.init_app(app) | |
| with express_session() as db_session: | |
| # build query object from express_session | |
| query = db_session.query(SalesRegistry.id, | |
| SalesRegistry.client, | |
| SalesRegistry.value, | |
| SalesRegistry.date | |
| ).filter(SalesRegistry.sales_status == 'COMPLETED').limit(50) | |
| # execute query on database from pandas | |
| df = pd.read_sql(query.statement, db_session.bind) | |
| # show loaded data on dataframe memory | |
| df.head() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment