Skip to main content

A tooltip functionality for Dash.

Project description

PyPI - Python Version CodeQL Downloads Pytest

Dash Tooltip

A module to add interactive editable tooltips to your Dash applications. Inspired by mplcursors and Matlab's datatip.

newplot(6)

Installation

You can download the dash_tooltip.py module and place it in your working directory.

Basic Usage

import numpy as np
import plotly.express as px
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from dash_tooltip import tooltip

# Sample Data
np.random.seed(20)
y1 = np.random.normal(0, 10, 50)
x1 = np.arange(0, 50)
fig1 = px.scatter(x=x1, y=y1)
fig1.update_layout(title_text="Editable Title", title_x=0.5)

app1 = Dash(__name__)

#makes graph items, including tooltips editable
app1.layout = html.Div([
    dcc.Graph(
        id='graph1',
        figure=fig1,
        config={
            'editable': True,
            'edits': {
                'shapePosition': True,
                'annotationPosition': True
            }
        }
    )
])

# Add the tooltip functionality to the app
tooltip(app1)

Click on data points to add tooltips. If dcc.Graph is configured editatble, tolltips:

  • can be dragged around
  • text can be edited on click
  • can be deleted: click, delete text, enter. In some occasions a tooltip arrow may remain due to a Dash bug (clientside_callback not firing). In this cas, click near arrow end (mouse cursor changes to pointer), enter some text and repeat deletion and enter.

Advanced Usage

If you want to customize the tooltips, hover templates, and more:

import pandas as pd
import numpy as np
import plotly.express as px
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from dash_tooltip import tooltip

# Generate random time series data
date_rng = pd.date_range(start='2020-01-01', end='2020-12-31', freq='h')
ts1 = pd.Series(np.random.randn(len(date_rng)), index=date_rng)
ts2 = pd.Series(np.random.randn(len(date_rng)), index=date_rng)
df = pd.DataFrame({'Time Series 1': ts1, 'Time Series 2': ts2})

template = "x: %{x}<br>y: %{y:.2f}<br>ID: %{pointNumber}<br>name: %{customdata[0]}<br>unit: %{customdata[1]}"
fig10 = px.line(df, x=df.index, y=df.columns, title="Time Series Plot")

for i, trace in enumerate(fig10.data):
    trace.customdata = np.column_stack((np.repeat(df.columns[i], len(df)), np.repeat('#{}'.format(i+1), len(df))))
    trace.hovertemplate = template

app10 = Dash(__name__)

app10.layout = html.Div([
    dcc.Graph(
        id="graph-id",
        figure=fig10,
        config={
            'editable': True,
            'edits': {
                'shapePosition': True,
                'annotationPosition': True
            }
        }
    )
])

tooltip(app10, graph_ids=["graph-id"], template=template, debug=True)

Tooltip Templates with Formatting

Tooltips can be formatted using templates similar to Plotly's hovertemplates. The tooltip template allows custom formatting and the inclusion of text and values.

For example, you can use a template like "x: %{x:.2f}<br>y: %{y:.2f}" to display the x and y values with two decimal places.

Refer to Plotly’s documentation on hover text and formatting for more details on how to construct and customize your tooltip templates.

Custom Styling

custom_style = {
        "font": {"size": 12, "color":"red"},
        "arrowcolor": "red",
        'arrow_size': 5,
        # ... any other customization
    }
tooltip(app10, style=custom_style, graph_ids=["graph-id"], template=template, debug=True)

For more examples, refer to the provided dash_tooltip_demo.py or its Jupyter counterpart dash_tooltip_demo.ipynb.

Handling Log Axes

Due to a long-standing bug in Plotly (see Plotly Issue #2580), annotations (fig.add_annotation) may not be placed correctly on log-scaled axes. The dash_tooltip module provides an option to automatically correct the tooltip placement on log-scaled axes via the apply_log_fix argument in the tooltip function. By default, apply_log_fix is set to True to enable the fix.

Debugging

If you encounter any issues or unexpected behaviors, enable the debug mode by setting the debug argument of the tooltip function to True. The log outputs will be written to dash_app.log in the directory where your script or application is located.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

  • Inspired by mplcursors and Matlab's datatip.

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

Dash_tooltip-0.3.5.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

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

Dash_tooltip-0.3.5-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file Dash_tooltip-0.3.5.tar.gz.

File metadata

  • Download URL: Dash_tooltip-0.3.5.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for Dash_tooltip-0.3.5.tar.gz
Algorithm Hash digest
SHA256 af0ca8ef8cf07316274bb9b87bde5fdd257516c2cdc36db49f5499aee9363978
MD5 f025e657eb9e83aaae6c825e6805176f
BLAKE2b-256 5e6764572244c396262e606692ca2614821148a4721c987caf24bc82d3335c66

See more details on using hashes here.

File details

Details for the file Dash_tooltip-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: Dash_tooltip-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for Dash_tooltip-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 94e7f47b88764315d1a0a6108cfdf2badaf2957e832e6aae44f2c65775f2ef6d
MD5 610f93a03c904fc33cc428ba59bd2baa
BLAKE2b-256 23f9a69856d20a21c3be85b88e410a46e0bea277eb7fec130716716e618c5e23

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