Skip to main content

dash-dynamic-images

A library that helps with embedding dynamic and generative images into Plotly Dash applications.

Setting up

1. Install the dash-dynamic-images python package

pip install dash-dynamic-images

2. Add an html.Img element to your page's layout

This step does not differ in any way from the regular process of writing layouts for Dash applications.

import dash_html_components as html

...

app.layout = html.Div(children=[
    html.Img(id='image'),
    ...
    dcc.Input(id='x', type='number', value=10),
    dcc.Input(id='y', type='number', value=10)
])

3. Create an image_callback that serves your dynamic image

You can use image_callback decorator to create callbacks that return dynamic images. It works similarly to the standard Dash callback decorator, but with few notable differences:

  • The first argument of the decorator should be an instance of the Dash object.
  • The callback should have only one output, pointing at the src property of an Img layout element.
  • The decorated function should return a PIL.Image.Image object (from the Pillow python library).
from dash_dynamic_images import image_callback
from PIL import Image, ImageDraw

...

@image_callback(
    app,
    dash.Output('image', 'src'),
    dash.Input('x', 'value'),
    dash.Input('y', 'value'))
def generate_image(x, y):
    image = Image.new('RGB', (200, 200), color=(0, 0, 200))
    ImageDraw.Draw(image).line([(0, 0), (x, y)], width=5)
    return image

As long as the returned object is a Pillow image, it does not matter on how was it create. You can generate it from scratch or obtain it from an external provider.

import requests

...

@image_callback(
    app,
    dash.Output('image', 'src'),
    dash.Input('button', 'n_clicks'))
def generate_image(_):
    response = requests.get('https://your_service.example/api/images/get')

    return Image.open(BytesIO(response.content))

Please consult the Pillow documentation for more details.

4. Enjoy the working application

A complete example of an application:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash_dynamic_images import image_callback
from PIL import Image, ImageDraw


app = dash.Dash()
app.layout = html.Div(children=[
    html.Img(id='image'),
    dcc.Input(id='x', type='number', value=10),
    dcc.Input(id='y', type='number', value=10)
])


@image_callback(
    app,
    dash.Output('image', 'src'),
    dash.Input('x', 'value'),
    dash.Input('y', 'value'))
def generate_image(x, y):
    image = Image.new('RGB', (200, 200), color=(0, 0, 200))
    ImageDraw.Draw(image).line([(0, 0), (x, y)], width=5)
    return image


if __name__ == '__main__':
    app.run_server(debug=True)

How it works

Whenever an image_callback is registered, the library performs two operations:

  • It registers a flask route with a path of /image_generator/{unique_guid}.png that generates and serves images whenever invoked.
  • It registers a standard Dash callback that produces and returns a parametrized (through the query string) image url based on the image_callback input values.

In practice, whenever one of the image_callback input parameters change, a new url is generated and inserted into the src property of the Img element, which in orders triggers a process of requesting and producing a new image.

The generated images are not persisted in the file system.

The library is aligned with the stateless nature of the Dash framework and therefor is compatible with its horizontal scaling capabilities (where a single application can be served by multiple processes and/or machines).

Motivation

This library aims at simplifying the process described in the previous section so that it can be achieved through a single line of a python code.

Metadata

Release files for dash-dynamic-images 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dash-dynamic-images 1.0.1
File Size Uploaded
dash-dynamic-images-1.0.1.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dash-dynamic-images 1.0.1
File Interpreter ABI Platform
dash_dynamic_images-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / dash-dynamic-images-1.0.1.tar.gz

Download URL dash-dynamic-images-1.0.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
94a0410481c0521832cc0a276ed3f8513223e2a5fd508c5b1af4ef3dfedd9490
BLAKE2b-256 checksum
How to use checksums
032e6ab0ea1c95a822f275374383ff93b8e7484baf16ab94730f8df4ad685157
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / dash_dynamic_images-1.0.1-py3-none-any.whl

Download URL dash_dynamic_images-1.0.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d5cb032f996d1254fb6ca810165ca64c3e60198e40189601ff251573561773b
BLAKE2b-256 checksum
How to use checksums
ab1674137b26fcde724e33d8173f3a48913c861b4a3f2e436ce06279ad5573fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page