Skip to main content

dodotable

Project description

Latest PyPI version Documentation Status https://travis-ci.org/spoqa/dodotable.svg?branch=master

HTML table representation for SQLAlchemy .

SQLAlchemy to <table>

Assume you have an entity called Music. It looks like the below.

class Music(Base):

    id = Column(Integer, primary_key=True)

    name = Column(Unicode, nullable=False)

The following code renders a sortable <table> consisting of a list of music.

from dodotable.schema import Table, Column

table = Table(
    cls=Music,
    label='music table',
    columns=[
        Column(attr='id', label=u'id', order_by='id.desc'),
        Column(attr='name', label=u'name'),
    ],
    sqlalchemy_session=session
)
print(table.select(offset=0, limit=10).__html__())

Using with Flask

Flask uses Jinja2 as the template engine. As they mentioned on document[1]_, it is one of strategy that implement __html__ on every class inherit dodotable.schema.Renderable to convert a instance into HTML directly in Jinja2. Re-write the example written before with Flask.

from dodotable.schema import Table, Column
from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/musics/', methods=['GET'])
def list_musics():
    table = Table(
        cls=Music,
        label='music table',
        columns=[
            Column(attr='id', label=u'id',
                   order_by=request.args.get('order_by')),
            Column(attr='name', label=u'name'),
        ],
        sqlalchemy_session=session
    )
    return render_template(
        'list_musics.html',
        table=table.select(limit=request.args.get('limit'),
                           offset=request.args.get('offset'))
    )

And list_musics.html which is jinja2 template is look like below.

<html>
  <body>
    {{ table }}
  </body>
</html>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dodotable-0.3.7.tar.gz (14.5 kB view details)

Uploaded Source

File details

Details for the file dodotable-0.3.7.tar.gz.

File metadata

  • Download URL: dodotable-0.3.7.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dodotable-0.3.7.tar.gz
Algorithm Hash digest
SHA256 82cc92d1e5974571eabf9e98340d3870fa7dc85d1ebab1595e3cf7c9990ac020
MD5 c1f48e2b6e97370e3c4997ca47cdfd44
BLAKE2b-256 8d029b4fdcf66c116d373c91e063973880f6541dfb27b10d18e830a80065b132

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page