Redirecting Logs to Plotly Dash
Plotly’s Dash has made it very easy to put together small dashboards; there are components for html and even custom react components that have een built onto of python objects. These can make it easy to display pandas dataframes, important metrics, or plots that update based on user interaction.
When you are writing applications where you want to capture some of the logs for users – especially for internal facing cases it can be useful to capture these in the page.
One of the ways that this can be done in Python is using the built-in logging. There are great utilities for capturing streams and saving them to rotating log files or displaying them on the console. Capturing these logs and displaying them to users within the application can help advanced users determine issues when interacting due to file corruption, formatting, etc. without writing complicated UI to capture this information. In dash, decorators are used to wrap functions to allow for functions to react to user inputs and generate outputs that then update the react components.
Python offers the robust logging handlers with three specific: StreamHandler, FileHandler, and NullHandler. These can be used to build out loggers as I will show below. The chocies are to either to redirect the stream to a file, read, and then display to the console or cpature the stream and display the most recent adding to the client state.
The above example dash application will append html divs to the Div with the id of log.