Skip to main content

Django app for building dashboards using raw SQL queries

Project description

django-sql-dashboard

PyPI Changelog License

Django app for building dashboards using raw SQL queries

Brings a useful subset of Datasette to Django.

Currently only works with PostgreSQL.

This is very early alpha. You should not yet trust this code, especially with regards to security. Do not run this in production (yet)!

Screenshot

Django_SQL_Dashboard screenshot

Installation

Install this library using pip:

$ pip install django-sql-dashboard

Usage

Add "django_sql_dashboard" to your INSTALLED_APPS in settings.py.

Define a "dashboard" database alias in settings.py. It should look something like this:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "mydb",
    },
    "dashboard": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "mydb",
        "OPTIONS": {"options": "-c default_transaction_read_only=on -c statement_timeout=100"},
    },
}

Even better: create a new PostgreSQL role that is limited to read-only SELECT access to a list of tables, following these instructions.

Add the following to your urls.py:

from django.urls import path, inclued
import django_sql_dashboard

urlpatterns = [
    # ...
    path("dashboard/", include(django_sql_dashboard.urls)),
]

Now visit /dashboard/ as a staff user to start trying out the dashboard.

SQL parameters

If your SQL query contains %(name)s parameters, django-sql-dashboard will convert those into form fields on the page and allow users to submit values for them. These will be correctly quoted and escaped in the SQL query.

Given the following SQL query:

select * from blog_entry where slug = %(slug)s

A form field called slug will be displayed, and the user will be able to use that to search for blog entries with that given slug.

Here's a more advanced example:

select * from location
where state_id = cast(%(state_id)s as integer)
and name ilike '%%' || %(search)s || '%%';

Here a form will be displayed with state_id and search fields.

The values provided by the user will always be treated like strings - so in this example the state_id is cast to integer in order to be matched with an integer column.

Any % characters - for example in the ilike query above - need to be escaped by providing them twice: %%.

Widgets

SQL queries default to displaying as a table. Other forms of display - called widgets - are also available, and are selected based on the names of the columns returned by the query.

Big number: big_namber, label

If you want to display the results as a big number accompanied by a label, you can do so by returning big_number and label columns from your query, for example.

select 'Number of states' as label, count(*) as big_number from state;

Custom widgets

You can define your own custom widgets by creating templates with special names.

Decide on the column names that you wish to customize for, then sort them alphabetically and join them with hyphens to create your template name.

For example, you could define a widget that handles results returned as placename, geojson by creating a template called geojson-label.html.

Save that in one of your template directories as django_sql_dashboard/widgets/geojson-label.html.

Any SQL query that returns exactly the columns placename and geojson will now be rendered by your custom template file.

Within your custom template you will have access to a template variable called result with the following keys:

  • result.sql - the SQL query that is being displayed
  • rows - a list of rows, where each row is an object that can be accessed by index or by column name, e.g. {{ row.big_number }}.
  • description - the psycopg2 cursor description
  • truncated - boolean, specifying whether the results were truncated (at 100 items) or not
  • duration_ms - how long the query took, in floating point milliseconds
  • templates - a list of templates that were considered for rendering this widget

You can find examples of widget templates in the templates/django_sql_dashboard/widgets directory.

Development

To contribute to this library, first checkout the code. Then create a new virtual environment:

cd django-sql-dashboard
python -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and tests:

pip install -e '.[test]'

To run the tests:

pytest

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

django-sql-dashboard-0.2a1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

django_sql_dashboard-0.2a1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file django-sql-dashboard-0.2a1.tar.gz.

File metadata

  • Download URL: django-sql-dashboard-0.2a1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for django-sql-dashboard-0.2a1.tar.gz
Algorithm Hash digest
SHA256 00156d4022d6b60c563d36f7a01ecd148a864e3f5b7240a7aa863df00402f229
MD5 1a65ac2a4015e797deeb1e9246bc86ca
BLAKE2b-256 0c02fb285a75e53e6d0fd6610b2426bc15ff45b230dfd91780d4ce702ba2e0a3

See more details on using hashes here.

File details

Details for the file django_sql_dashboard-0.2a1-py3-none-any.whl.

File metadata

  • Download URL: django_sql_dashboard-0.2a1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for django_sql_dashboard-0.2a1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9649063d986ce66c5f9420a7031173ffc51ad5502ef5e2259c6a0ecc6fe0bcb
MD5 0d49380bda258d17f96cd8ab4c08a44e
BLAKE2b-256 454f1c5bb2a4409784c85d2dac6d698e6d27dd4a60debf9b596495100327dbee

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