Skip to main content

Embedded Flask-Admin list views.

Project description

Flask-Admin-Subview

Embed Flask-Admin list views to an arbitrary page:

https://raw.githubusercontent.com/artemShelest/flask-admin-subview/master/res/screen1.png https://raw.githubusercontent.com/artemShelest/flask-admin-subview/master/res/screen2.png

Limitations

  • Inline edits are not supported

  • Bootstrap3 templates only

Installation

pip install flask-admin-subview

Integration

The easiest way to integrate is to use helpers for the details view of the model. The following example demonstrates integration of subview to show relations of SQLAlchemy model in the details page.

DB Schema

class ContentModel(db.Model):
    __table__ = "content"
    id = db.Column(db.Integer, primary_key=True)
    container_id = db.Column(db.Integer, db.ForeignKey("container.id"), nullable=False)

class ContainerModel(db.Model):
    __table__ = "container"
    id = db.Column(db.Integer, primary_key=True)
    content = db.relationship(ContentModel)

Prepare your subview

It is a good idea to subclass existing view of your model:

import flask_admin_subview

class ContentModelSubview(flask_admin_subview.View, ContentModelView):
    pass

Or you can create a brand-new view for the subview:

from flask_admin.contrib.sqla import ModelView
import flask_admin_subview

class ContentModelSubview(flask_admin_subview.View, ModelView):
    pass

Add query filter to show content for certain container only, container id will be passed as a URL parameter:

class ContentModelSubview(...):
    def get_query(self):
        return self._extend_query(super(ContentModelSubview, self).get_query())

    def get_count_query(self):
        return self._extend_query(super(ContentModelSubview, self).get_count_query())

    def _extend_query(self, query):
        container_id = request.args.get('id')
        if container_id is None:
            abort(400, "Container id required")
        return query.filter(ContentModel.container_id == container_id)

Initialize an extension

from flask_admin_subview import Subview

app = Flask(__name__)
admin = Admin(app, template_mode="bootstrap3")
# only supports bootstrap3 mode
Subview(app, template_mode="bootstrap3")

Add your subview as a blueprint

app = Flask(__name__)
# ...
app.register_blueprint(
    ContentModelSubview(Content, db.session, "Content", endpoint="content_subview").
    create_blueprint(admin))

Prepare container view

Use helper to display subview in the model’s details:

from flask_admin_subview import SubviewContainerMixin, SubviewEntry

class ContainerView(SubviewContainerMixin, ModelView):
    can_view_details = True
    subviews = (
        # specify that we need to pass id from the location URL to the subview
        SubviewEntry("/admin/content_subview/", "Content Subview", "id"),
    )

TODO

  • Add tests

  • Add example app code comments

  • Add Bootstrap2 templates

  • Possibly, support inline edits

  • Describe advanced usage

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

Flask-Admin-Subview-0.1.2.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

Flask_Admin_Subview-0.1.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Admin-Subview-0.1.2.tar.gz.

File metadata

  • Download URL: Flask-Admin-Subview-0.1.2.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for Flask-Admin-Subview-0.1.2.tar.gz
Algorithm Hash digest
SHA256 45d5df37fcb3f0e74220bddd894039ab21c9d1e9b380144d68d92b403a721ffd
MD5 a1266e87fbacac982da77c3c4a175b68
BLAKE2b-256 f2a058e99a62d189fbf16023fe4f25571103b3dc80feec00c1176199742c4688

See more details on using hashes here.

File details

Details for the file Flask_Admin_Subview-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Admin_Subview-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b651e79a3f8918e1606beef904a6330757e2a58f2978777a39c845f9445b009d
MD5 cc6f75f8ae86e68ad9a51b094a7f9556
BLAKE2b-256 b84ae434d90104afb88b777f603dc0850db2e41b996af21e2c96859eb051678a

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