如何从sqlalchemy获取元数据以表格形式在Jinja中显示?

时间:2018-12-07 10:28:51

标签: python flask sqlalchemy jinja2 flask-sqlalchemy

我在postgres和mysql中有一些数据库。我希望用户通过提供凭据连接到数据库,然后在他正在查看的html上显示表的元数据(作为表)。

这是相关的代码。

meta = MetaData()
meta.reflect(bind=eng)

return str(meta.tables)

这将返回以下类型的<class 'sqlalchemy.util._collections.immutabledict'>

immutabledict({'categories': Table('categories', MetaData(bind=None), Column('category', INTEGER(), table=, primary_key=True, nullable=False, server_default=DefaultClause(, for_update=False)), Column('categoryname', VARCHAR(length=50), table=, nullable=False), schema=None), 'inventory': Table('inventory', MetaData(bind=None), Column('prod_id', INTEGER(), table=, primary_key=True, nullable=False), Column('quan_in_stock', INTEGER(), table=, nullable=False), Column('sales', INTEGER(), table=, nullable=False), schema=None), 'products': Table('products', MetaData(bind=None), Column('prod_id', INTEGER(), table=, primary_key=True, nullable=False, server_default=DefaultClause(, for_update=False)), Column('category', INTEGER(), table=, nullable=False), Column('title', VARCHAR(length=50), table=, nullable=False), Column('actor', VARCHAR(length=50), table=, nullable=False), Column('price', NUMERIC(precision=12, scale=2), table=, nullable=False), Column('special', SMALLINT(), table=), Column('common_prod_id', INTEGER(), table=, nullable=False), schema=None), 'reorder': Table('reorder', MetaData(bind=None), Column('prod_id', INTEGER(), table=, nullable=False), Column('date_low', DATE(), table=, nullable=False), Column('quan_low', INTEGER(), table=, nullable=False), Column('date_reordered', DATE(), table=), Column('quan_reordered', INTEGER(), table=), Column('date_expected', DATE(), table=), schema=None), 'cust_hist': Table('cust_hist', MetaData(bind=None), Column('customerid', INTEGER(), ForeignKey('customers.customerid'), table=, nullable=False), Column('orderid', INTEGER(), table=, nullable=False), Column('prod_id', INTEGER(), table=, nullable=False), schema=None), 'customers': Table('customers', MetaData(bind=None), Column('customerid', INTEGER(), table=, primary_key=True, nullable=False, server_default=DefaultClause(, for_update=False)), Column('firstname', VARCHAR(length=50), table=, nullable=False), Column('lastname', VARCHAR(length=50), table=, nullable=False), Column('address1', VARCHAR(length=50), table=, nullable=False), Column('address2', VARCHAR(length=50), table=), Column('city', VARCHAR(length=50), table=, nullable=False), Column('state', VARCHAR(length=50), table=), Column('zip', INTEGER(), table=), Column('country', VARCHAR(length=50), table=, nullable=False), Column('region', SMALLINT(), table=, nullable=False), Column('email', VARCHAR(length=50), table=), Column('phone', VARCHAR(length=50), table=), Column('creditcardtype', INTEGER(), table=, nullable=False), Column('creditcard', VARCHAR(length=50), table=, nullable=False), Column('creditcardexpiration', VARCHAR(length=50), table=, nullable=False), Column('username', VARCHAR(length=50), table=, nullable=False), Column('password', VARCHAR(length=50), table=, nullable=False), Column('age', SMALLINT(), table=), Column('income', INTEGER(), table=), Column('gender', VARCHAR(length=1), table=), schema=None), 'orders': Table('orders', MetaData(bind=None), Column('orderid', INTEGER(), table=, primary_key=True, nullable=False, server_default=DefaultClause(, for_update=False)), Column('orderdate', DATE(), table=, nullable=False), Column('customerid', INTEGER(), ForeignKey('customers.customerid'), table=), Column('netamount', NUMERIC(precision=12, scale=2), table=, nullable=False), Column('tax', NUMERIC(precision=12, scale=2), table=, nullable=False), Column('totalamount', NUMERIC(precision=12, scale=2), table=, nullable=False), schema=None), 'orderlines': Table('orderlines', MetaData(bind=None), Column('orderlineid', INTEGER(), table=, nullable=False), Column('orderid', INTEGER(), ForeignKey('orders.orderid'), table=, nullable=False), Column('prod_id', INTEGER(), table=, nullable=False), Column('quantity', SMALLINT(), table=, nullable=False), Column('orderdate', DATE(), table=, nullable=False), schema=None)})

我已经在Jinja模板中对此进行了for循环,但它显示了sqlalchemy 'table' object is not iterable

如何从数据库中获取元数据以在html中显示为表格?

1 个答案:

答案 0 :(得分:0)

inspector类有助于访问元数据。

from sqlalchemy import create_engine
from sqlalchemy.engine import reflection
engine = create_engine('...')
insp = reflection.Inspector.from_engine(engine)
print(insp.get_table_names())

可用的其他方法是

get_check_constraints
get_columns
get_foreign_keys
get_indexes
get_pk_constraint
get_schema_names

有关说明和更多方法,请参考文档。然后,您可以将变量传输到jinja模板并进行相应打印。

这是我在工作示例中所做的。

tablenames = insp.get_table_names()
return render_template('tables.html', inspector=insp, table=tablenames)

Jinja:

<table>
    <tr>
        <th>table</th>
        <th>name</th>
        <th>type</th>
        <th>nullable</th>
        <th>default</th>
        <th>autoincrement</th>
        <th>comment</th>
    </tr>
    {% for table in table %}
        {% for col in inspector.get_columns(table_name=table) %}
            <tr>
            <td>{{ table }}</td>
            {% for val in col.values() %}
                <td>{{ val }}</td>
            {% endfor %}
            </tr>
        {% endfor %}
    {% endfor %}
</table>