When developing an application, sometimes it is helpful to log application events and browse them as the application is being used.
dstack offers a simple way to log application events and access them from the interface.
The easiest way of logging application events is by printing them into
stdout (e.g. using
print()). Here's an example:
import dstack as dsimport plotly.express as pxapp = ds.app() # Сreate an instance of the application# An utility function that loads the datadef get_data():return px.data.stocks()# A drop-down control that shows stock symbolsstock = app.select(items=get_data().columns[1:].tolist())# A handler that updates the plot based on the selected stockdef output_handler(self, stock):print("Calling output_handler") # Log a messagesymbol = stock.value() # the selected stock# a plotly line chart where the X axis is date and Y is the stock's priceself.data = px.line(get_data(), x='date', y=symbol)# A plotly chart outputapp.output(handler=output_handler, depends=[stock], require_apply=True)# Deploy the application with the name "stocks_logs" and print its URLurl = app.deploy("stocks_logs")print(url)