Skip to main content

Unofficial backend utilities for using Material-UI's X-Data-Grid component

Project description

MUI Data Grid

This is an unofficial toolbox to make integrating a Python web application with Material UI's data grid simpler.

Documentation

Requirements

  • Python 3.7+

Features

  • Grid Sort Model support
  • Grid Filter Model support (partial: missing quick filter support)
  • Grid Pagination Model support (LIMIT / OFFSET based, cursor not currently supported)
  • Flask integration
  • SQLAlchemy integration

Installation

Pip

python -m pip install -U 'mui-data-grid'

or with extras:

python -m pip install -U 'mui-data-grid[flask]'
python -m pip install -U 'mui-data-grid[sqlalchemy]'
python -m pip install -U 'mui-data-grid[flask, sqlalchemy]'

Poetry

poetry add mui-data-grid

Usage

Integrations

Flask

#!/usr/bin/env python
# examples/main.py

from flask import Flask, jsonify
from flask.wrappers import Response

from mui.v5.integrations.flask import get_grid_models_from_request

app = Flask(__name__)

FILTER_MODEL_KEY = "filter_model"
SORT_MODEL_KEY = "sort_model[]"
PAGINATION_MODEL_KEY = None  # stored inline in the query string, not encoded as an obj


@app.route("/")
def print_sorted_details() -> Response:
    # models will return default values if the keys don't exist,
    # so you can choose what features you integrate, and when.
    models = get_grid_models_from_request(
        filter_model_key=FILTER_MODEL_KEY,
        pagination_model_key=PAGINATION_MODEL_KEY,
        sort_model_key=SORT_MODEL_KEY,
    )
    return jsonify(
        {
            # sort_model is a list[GridSortItem]
            SORT_MODEL_KEY: [model.dict() for model in models.sort_model],
            # filter_model is GridFilterModel
            FILTER_MODEL_KEY: models.filter_model.dict(),
            # pagination_model is a GridPaginationModel
            # providing a consistent interface to pagination parameters
            PAGINATION_MODEL_KEY: models.pagination_model,
        }
    )


if __name__ == "__main__":
    app.run()

SQLAlchemy

    # please see examples/main.py for the full code
    models = get_grid_models_from_request(
        filter_model_key=FILTER_MODEL_KEY,
        pagination_model_key=PAGINATION_MODEL_KEY,
        sort_model_key=SORT_MODEL_KEY,
    )
    session = Session()
    try:
        base_query = session.query(ExampleModel)
        dg_query = apply_request_grid_models_to_query(
            query=base_query,
            request_model=models,
            column_resolver=example_model_resolver,
        )
        # we calculate total separately so that we can re-use the result
        # rather than have .pages() fire off an additional db query.
        total = dg_query.total()
        def item_factory(item: ExampleModel) -> Dict[str, int]:
            return item.dict()
        return jsonify(
            {
                "items": dg_query.items(factory=item_factory),
                "page": dg_query.page,
                "pageSize": dg_query.page_size,
                "pages": dg_query.pages(total=total),
                "total": total,
            }
        )
    finally:
        session.close()

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

mui_data_grid-0.8.2.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mui_data_grid-0.8.2-py3-none-any.whl (42.3 kB view details)

Uploaded Python 3

File details

Details for the file mui_data_grid-0.8.2.tar.gz.

File metadata

  • Download URL: mui_data_grid-0.8.2.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.11.1 Darwin/21.6.0

File hashes

Hashes for mui_data_grid-0.8.2.tar.gz
Algorithm Hash digest
SHA256 3305930c7fcb66abe157a0947620b96dacaff08f105c045c48aaed2af6cf5b35
MD5 2a387f82b146d83e4a4dfaf4ac642f47
BLAKE2b-256 b247c4b0fc56109e948ad2ca21e1c838b2cef2fd586cc3d96b893d79ae635847

See more details on using hashes here.

File details

Details for the file mui_data_grid-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: mui_data_grid-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 42.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.11.1 Darwin/21.6.0

File hashes

Hashes for mui_data_grid-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 110ebc2076490c9b96c7d952d164ea0ddc016b4e5c6a6bc822eefd8e0655a4e8
MD5 16b3ee3fb17204630a31a19608fe9536
BLAKE2b-256 0a9f65e540997b1c4e51bdae9ae2f10d00412575a8dd5d91f17c70092793c836

See more details on using hashes here.

Supported by

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