regions = ctrl.ComboBox(items=get_regions, label="Region") countries = ctrl.ComboBox(handler=countries_handler, label="Country", depends=[regions]) def country_output_handler(self: ctrl.Output, countries: ctrl.ComboBox): df = get_data() self.data = df[df["Country"] == countries.value()] data_by_country_app = ds.app( controls=[regions, countries], outputs=[ctrl.Output(handler=country_output_handler, depends=[countries])]) def get_companies(): df = get_data() return df["Company"].unique().tolist() companies = ctrl.ComboBox(items=get_companies, label="Company") def company_output_handler(self: ctrl.Output, companies: ctrl.ComboBox): df = get_data() row = df[df["Company"] == companies.value()].filter( ["y2015", "y2016", "y2017", "y2018", "y2019"], axis=1) row.rename(columns={
import dstack.controls as ctrl import dstack as ds import plotly.express as px @ds.cache() def get_data(): return px.data.gapminder() def output_handler(self: ctrl.Output, year: ctrl.Slider): year = year.values[year.selected] self.data = px.scatter(get_data().query("year==" + str(year)), x="gdpPercap", y="lifeExp", size="pop", color="country", hover_name="country", log_x=True, size_max=60) app = ds.app(controls=[ ctrl.Slider(values=get_data()["year"].unique().tolist(), require_apply=False) ], outputs=[ctrl.Output(handler=output_handler)]) result = ds.push('controls/slider', app) print(result.url)
import dstack.controls as ctrl import dstack as ds import plotly.express as px @ds.cache() def get_data(): return px.data.stocks() def symbols_handler(self: ctrl.ComboBox): print("Calling symbols_handler") self.items = get_data().columns[1:].tolist() def output_handler(self, ticker): print("Calling output_handler") self.data = px.line(get_data(), x='date', y=ticker.value()) app = ds.app(controls=[(ctrl.ComboBox(handler=symbols_handler))], outputs=[(ctrl.Output(handler=output_handler))]) result = ds.push("logs", app) print(result.url)
import dstack as ds import dstack.controls as ctrl import pandas as pd def app_handler(self: ctrl.Output, uploader: ctrl.FileUploader): if len(uploader.uploads) > 0: with uploader.uploads[0].open() as f: self.data = pd.read_csv(f).head(100) else: self.data = ds.md("No file selected") app = ds.app(controls=[ctrl.FileUploader(label="Select a CSV file")], outputs=[ctrl.Output(handler=app_handler)]) url = ds.push("controls/file_uploader", app) print(url)
import dstack.controls as ctrl import dstack as ds import plotly.express as px @ds.cache() def get_data(): return px.data.stocks() def output_handler(self, ticker): self.data = px.line(get_data(), x='date', y=ticker.value()) app = ds.app( controls=[(ctrl.ComboBox(items=get_data().columns[1:].tolist()))], outputs=[ ctrl.Output(data=ds.md( "Here's a simple application with **Markdown** and a chart.")), ctrl.Output(handler=output_handler) ]) result = ds.push("markdown", app) print(result.url)
import dstack as ds import dstack.controls as ctrl from handlers import fake_handler app = ds.app(outputs=[ctrl.Output(handler=fake_handler)], depends=["handlers", "utils"], requirements="requirements.txt") # An equal alternative to this is the following: # ds.app(outputs=[ctrl.Output(handler=fake_handler)], depends=["numpy", "pandas", "faker==5.5.0", "handlers", "utils"]) url = ds.push("depends", app) print(url)