Hi all,
In a PyShiny app, I would like to upload a file, read the contents of the file into chunks, and display them in tabs.
The file is a simple json
file:
{ "FruitData": { "name": "Orange", "attributes": { "colour": "orange" } },
"VegetableData": { "name": "Broccoli", "attributes": { "colour": "green" } },
"NoodleData": { "name": "Ramen", "attributes": { "colour": "yellow" } }
}
My app.py
looks like this:
from shiny import *
from shiny.types import FileInfo
import json
app_ui = ui.page_fluid(
ui.input_file("file1", "Choose a file to upload:"),
ui.output_ui("contents"),
ui.navset_tab(
ui.nav("Fruit data", ui.output_text_verbatim("fruit_data")),
ui.nav("Vegetable data", ui.output_text_verbatim("veg_data")),
),
)
def server(input, output, session):
@output
@render.text
def contents():
if input.file1() is None:
return "Please upload data file"
f: list[FileInfo] = input.file1()
with open(f[0]["datapath"]) as filehandle:
data=json.loads(filehandle.read())
fruit_data=data["FruitData"]
veg_data=data["VegetableData"]
return fruit_data, veg_data
app = App(app_ui, server)
This is failing (obv) on the fact that contents
is returning the data, but the ui.output_text_verbatim
fields want the fruit_data
and veg_data
to be separate from contents
.
I have been reading the PyShiny example for input_file and PyShiny API doc on input_file.
I've tried using ui.update_text without success - I think it wants an ui.input_text
rather than an ui.output_text_verbatim
.
I also tried implementing something using the example on the reactive.Calc API documentation because that looked close too, but it failed because I couldn't pass the data to the function:
@reactive.Calc
def fruit_data(data):
return data["FruitData"]
I'm new to shiny and pyshiny and feel like I'm missing something simple.