Coder Social home page Coder Social logo

dash-vega-components's Introduction

Dash Vega Components

With the Vega component, you can display Vega-Altair charts in your Plotly Dash application. All features work as well with Vega-Lite and Vega specifications but the remainder of this README will focus on Altair as it is more common.

You can either go through the examples below or head over to the Plotly Dash documentation to learn more.

pip install dash-vega-components

Altair example

For the example below, you'll also need:

pip install altair vega_datasets
import altair as alt
from dash import Dash, Input, Output, callback, dcc, html
from vega_datasets import data

import dash_vega_components as dvc

# Passing a stylesheet is not required
app = Dash(
    __name__, external_stylesheets=["https://codepen.io/chriddyp/pen/bWLwgP.css"]
)

app.layout = html.Div(
    [
        html.H1("Altair Chart"),
        dcc.Dropdown(["All", "USA", "Europe", "Japan"], "All", id="origin-dropdown"),
        # Optionally, you can pass options to the Vega component.
        # See https://github.com/vega/vega-embed#options for more details.
        dvc.Vega(id="altair-chart", opt={"renderer": "svg", "actions": False}),
    ]
)


@callback(Output("altair-chart", "spec"), Input("origin-dropdown", "value"))
def display_altair_chart(origin):
    source = data.cars()

    if origin != "All":
        source = source[source["Origin"] == origin]

    chart = (
        alt.Chart(source)
        .mark_circle(size=60)
        .encode(
            x="Horsepower",
            y="Miles_per_Gallon",
            color=alt.Color("Origin").scale(domain=["Europe", "Japan", "USA"]),
            tooltip=["Name", "Origin", "Horsepower", "Miles_per_Gallon"],
        )
        .interactive()
    )
    return chart.to_dict()


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

Example

You can also pass a Vega or Vega-Lite specification as a dictionary.

Callbacks

Parameters are the basic building blocks to make an Altair chart interactive. They can either be simple variables or more complex selections that map user input (e.g., mouse clicks and drags) to data queries. In Vega, these are called Signals and the two concepts of Signals and Parameters are closely linked. As an Altair user, you don't have to know the details and you can think of them as synonyms.

You can trigger a Dash callback based on changes in any parameter which is defined in an Altair chart. To do this, you'll need to

  • specify a name when defining the parameter, for example alt.param(name="my_param") or alt.selection_point(name="my_param")
  • add the parameter name to the signalsToObserve property of the Vega component: dvc.Vega(id="chart1", signalsToObserve=["my_param]). If you want to observe all signals, you can also pass signalsToObserve=["all"]
  • use Input("chart1", "signalData") in your callback to access the value of "my_param" and react to changes

For more examples, see example_app.py which shows how to filter a pandas dataframe based on a selection in a chart and display it in a Dash data table (the same would work with the Dash AG Grid component), or head over to #5.

Some ideas of what you could do with this:

  • Filter a Dash data table based on the selected points in a scatter plot (see example_app.py)
  • Based on a clickable bar chart, update other charts in your application
  • If you have geographic data, show an overview map of aggregated regional data. Use this map as a navigation element in a dash multi-page app so that if a user clicks on e.g. the US, they get to the US specific subpage
  • ...

Further information

For more infos on the properites of the Vega component, see its docstring in Vega.py.

To learn more about making Altair charts interactive, see Interactive Charts - Vega-Altair docs.

If you want your chart to resize responsively, set container="width" in your Altair chart.

Development

Requires npm

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt

npm install
npm run build
# Test package with
python example_app.py

Visit http://localhost:8050 in your web browser

To cut a new release, see RELEASING.md

This package is based on the dash-component-boilerplate template.

dash-vega-components's People

Contributors

binste avatar

Stargazers

 avatar shinession avatar Feffery avatar InteractDA avatar Alexander Kiefer avatar  avatar Bayees avatar Karan M avatar Jakob Thoms avatar  avatar Will Sanger avatar  avatar Troy Yang avatar Will Dean avatar  avatar  avatar Márcio dos Santos avatar Stuart Axelbrooke avatar Anton Lobachev avatar Liripo avatar Klaus Eckelt avatar timelyportfolio avatar Joel Ostblom avatar Jon Mease avatar João Palmeiro avatar Dominik Moritz avatar  avatar wygud avatar  avatar  avatar Nathan Drezner avatar Ann Marie Ward avatar  avatar  avatar  avatar Firas Moosvi avatar Ned Letcher avatar  avatar

Watchers

 avatar  avatar

dash-vega-components's Issues

Setting 100% width on the Vega container by default?

Thanks for this great dash extension @binste ! It's saving me from so many iframe headaches =)

I wonder if it would make sense to set style={'width': '100%'} by default on the Vega component, so that there is only one step required to make the altair chart stretch the width of the container. Or is there any advantage of not having this by default?

One step further would be to provide a "dash-style" altair theme that sets width='container' for all charts when activated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.