Hello!
I am trying to display dynamic payloads coming from an application on to a card and allows user to download it as well. The payload is in JSON format. I am unable to display formatted JSON and could not figure out how to download in Vizro. Please review the code below and provide your inputs.
Thank you ![]()
"""Example app to show all features of Vizro."""
import json
import pandas as pd
import vizro.models as vm
from vizro import Vizro
from vizro.models.types import capture
# GLOBALS --------------------------------------------------------------------->
DROPDOWN_LINK_OPTIONS = [
{
"label": "Google",
"value": "google",
},
{
"label": "Vizro GitHub",
"value": "github",
},
{
"label": "Vizro Docs",
"value": "docs",
},
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{"id": "1001", "type": "Regular"},
{"id": "1002", "type": "Chocolate"},
{"id": "1003", "type": "Blueberry"},
{"id": "1004", "type": "Devil's Food"}
]
},
"topping":
[
{"id": "5001", "type": "None"},
{"id": "5002", "type": "Glazed"},
{"id": "5005", "type": "Sugar"},
{"id": "5007", "type": "Powdered Sugar"},
{"id": "5006", "type": "Chocolate with Sprinkles"},
{"id": "5003", "type": "Chocolate"},
{"id": "5004", "type": "Maple"}
]
}
]
# incorporating data frame
dropdown_options = pd.DataFrame()
dropdown_options = dropdown_options._append(DROPDOWN_LINK_OPTIONS, ignore_index=True)
# CUSTOM COMPONENTS ---------------------------------------------------------->
@capture("action")
def display_selection(dropdown_value):
text = 'did not selected Google'
if dropdown_value == 'Google':
dropdown_options_str = json.loads(json.dumps(dropdown_options.to_dict()))
text = dropdown_options_str
print(text)
return text
# CUSTOM ACTION ------------------------------------------------------------->
vm.Page.add_type("components", vm.Dropdown)
# PAGE ----------------------------------------------------------------------->
page = vm.Page(
title="Page Title",
layout=vm.Layout(grid=[
[0, 1]
]),
components=[
vm.Dropdown(
id='custom_dropdown',
options=['Google', 'Vizro Docs', 'Vizro GitHub'],
value='Vizro Docs',
multi=False,
actions=[
vm.Action(
function=display_selection(),
inputs=["custom_dropdown.value"],
outputs=["display.children"],
)
],
),
vm.Card(id="display",
text="""json goes here when you select google in dropdown"""
),
],
)
dashboard = vm.Dashboard(pages=[page])
if __name__ == "__main__":
Vizro().build(dashboard).run()