I haven't been able to find an example in python that shows what I'm trying to do. I have a plot generated by a function within the app that I'd like to be able to download as a PNG file.
Various methods I've tried have generated empty files that cannot be opened, but that's as close as I've gotten. Has anyone been able to do this successfully?
Here's an incomplete example that is scaled down quite a bit from what I'm actually trying to do:
from datetime import date
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from shiny import App, Inputs, Outputs, Session, ui, render
app_ui = ui.page_fluid(
ui.output_plot('testvis'),
ui.download_button("downloadData", "Download"),
)
def server(input: Inputs, output: Outputs, session: Session):
@output
@render.plot
def testvis():
# Sample data in a DataFrame
data = {'x': [1, 2, 3, 4, 5],
'y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
# Create a simple line plot from the DataFrame
plot = df.plot(x='x', y='y', marker='o', linestyle='-', color='b')
return plot
@session.download(
filename=lambda: f"testdownload-{date.today().isoformat()}-{np.random.randint(100,999)}.png"
)
def downloadData():
yield ## this is where I need help
app = App(app_ui, server)