Skip to main content

Django use of plotly dash apps through template tags

Project description

django-plotly-dash

PyPI version Downloads Develop Branch Build Status Coverage Status Documentation Status

Expose plotly dash apps as Django tags. Multiple Dash apps can then be embedded into a single web page, persist and share internal state, and also have access to the current user and session variables.

See the source for this project here: https://github.com/GibbsConsulting/django-plotly-dash

This README file provides a short guide to installing and using the package, and also outlines how to run the demonstration application.

More detailed information can be found in the online documentation at https://readthedocs.org/projects/django-plotly-dash

Installation

First, install the package. This will also install plotly and some dash packages if they are not already present.

pip install django_plotly_dash

Then, just add django_plotly_dash to INSTALLED_APPS in your Django settings.py file

INSTALLED_APPS = [
    ...
    'django_plotly_dash.apps.DjangoPlotlyDashConfig',
    ...
    ]

Note that this package requires version 2.0 or greater of Django, due to the use of the path function for registering routes.

Live updating of applications, to share application state, requires further configuration. See the online documentation for more details.

Demonstration

The source repository contains a demo application. To clone the repo and lauch the demo:

git clone https://github.com/GibbsConsulting/django-plotly-dash.git

cd django-plotly-dash

./make_env                # sets up a virtual environment for development
                          #   with direct use of the source code for the package

./prepare_redis           # downloads a redis docker container
                          #   and launches it with default settings
                          #   *THIS STEP IS OPTIONAL*

./prepare_demo            # prepares and launches the demo
                          #   using the Django debug server at http://localhost:8000

Usage

To use existing dash applications, first register them using the DjangoDash class. This replaces the Dash class of the dash package.

Taking a very simple example inspired by the excellent getting started documentation:

import dash
import dash_core_components as dcc
import dash_html_components as html

from django_plotly_dash import DjangoDash

app = DjangoDash('SimpleExample')

app.layout = html.Div([
    dcc.RadioItems(
        id='dropdown-color',
        options=[{'label': c, 'value': c.lower()} for c in ['Red', 'Green', 'Blue']],
        value='red'
    ),
    html.Div(id='output-color'),
    dcc.RadioItems(
        id='dropdown-size',
        options=[{'label': i, 'value': j} for i, j in [('L','large'), ('M','medium'), ('S','small')]],
        value='medium'
    ),
    html.Div(id='output-size')

])

@app.callback(
    dash.dependencies.Output('output-color', 'children'),
    [dash.dependencies.Input('dropdown-color', 'value')])
def callback_color(dropdown_value):
    return "The selected color is %s." % dropdown_value

@app.callback(
    dash.dependencies.Output('output-size', 'children'),
    [dash.dependencies.Input('dropdown-color', 'value'),
     dash.dependencies.Input('dropdown-size', 'value')])
def callback_size(dropdown_color, dropdown_size):
    return "The chosen T-shirt is a %s %s one." %(dropdown_size,
                                                  dropdown_color)

Note that the DjangoDash constructor requires a name to be specified. This name is then used to identify the dash app in templates:

{% load plotly_dash %}

{% plotly_app name="SimpleExample" %}

The registration code needs to be in a location that will be imported into the Django process before any model or template tag attempts to use it. The example Django application in the demo subdirectory achieves this through an import in the main urls.py file; any views.py would also be sufficient.

Whilst this example allows for the direct use of existing Dash applications, it does not provide for the sharing or updating of internal state. The online documentation provides details on using these and other additional features.

Development

The make_env script sets up the development environment, and pulls in the packages specified in the dev_requirements.txt file. The check_code script invokes the test suite (using pytest) as well as invoking pylint on both the package and the associated demo.

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-plotly-dash-0.9.6.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

django_plotly_dash-0.9.6-py3-none-any.whl (38.3 kB view details)

Uploaded Python 3

File details

Details for the file django-plotly-dash-0.9.6.tar.gz.

File metadata

  • Download URL: django-plotly-dash-0.9.6.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.5.2

File hashes

Hashes for django-plotly-dash-0.9.6.tar.gz
Algorithm Hash digest
SHA256 528750d2f85d457e69da3deae14e983a8f0b5886aaa36dd149c92ab8e68dab8b
MD5 4cf487ac17bce6f3d550f0f3d3d814e6
BLAKE2b-256 36217e4074cc6867e9af9348cd00990a2370ecb68779d82eafa340a3508ee732

See more details on using hashes here.

Provenance

File details

Details for the file django_plotly_dash-0.9.6-py3-none-any.whl.

File metadata

  • Download URL: django_plotly_dash-0.9.6-py3-none-any.whl
  • Upload date:
  • Size: 38.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.5.2

File hashes

Hashes for django_plotly_dash-0.9.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8cc4288e613a7524774c4343b2664d4ba06f60b3c6921633fa3696eb63491f2e
MD5 65db90a35ba5378c7301279bd2688a2d
BLAKE2b-256 c7079cbe6e5a23db89ccf741e9b746f6f615bafe961a19ab4c0cd765e3ec60c1

See more details on using hashes here.

Provenance

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