Code Monkey home page Code Monkey logo

flask-admin-subview's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.