Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dash Vega Components #492

Open
Harizliewzw opened this issue Feb 23, 2024 · 1 comment
Open

Dash Vega Components #492

Harizliewzw opened this issue Feb 23, 2024 · 1 comment
Labels
question Further information is requested

Comments

@Harizliewzw
Copy link

Harizliewzw commented Feb 23, 2024

Altair is another popular visualisation tool that have been added as part of Dash Components.

See: (https://dash.plotly.com/dash-vega-components)

Following the example in the url above with slight modification to use DjangoDash instead of Dash:

import altair as alt
from dash import Dash, Input, Output, callback, dcc, html
from vega_datasets import data
import dash_bootstrap_components as dbc
from django_plotly_dash import DjangoDash
import dash_vega_components as dvc


app = DjangoDash('AltairExample', 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-chart2", opt={"renderer": "svg", "actions": False}),
    ]
)


@app.callback(Output("altair-chart2", "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()

.html example

{% extends 'base.html' %}
{% load static %}
{% block content %}
    {% load plotly_dash %}
    
    <div class="container">
        {% plotly_app name='AltairExample' ratio=1 %}
    </div>

    <br>
{% endblock %}

The iframe does not render. Anyone has any luck with implementing altair with dash on django?

@delsim
Copy link
Contributor

delsim commented Feb 24, 2024

@Harizliewzw when you try to view the app, do you get any error messages from the server or in the developer console of the browser?

@GibbsConsulting GibbsConsulting added the question Further information is requested label Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants