Are there examples of how to create custom st.column_config for st.dataframe?
I have a Pandas DataFrame with a column of SMILES strings for molecules (for example, “CCCNC”), and I’d like to display that column not as strings, but as molecular images instead. There are libraries that convert SMILES strings to images using JavaScript: SmilesDrawer and RDKit.js.
For SmilesDrawer, it looks like it might be enough to do the following:
replace SMILES strings (“CCCNC” for example) with HTML <canvas data-smiles="CCCNC"></canvas>,
import SmilesDrawer with <script src="https://unpkg.com/smiles-drawer@1.0.10/dist/smiles-drawer.min.js"></script> ,
and call function SmilesDrawer.apply();
But I’m not sure how to put this together in Streamlit, or whether it is possible to do so at all. Any advice, or pointers where to start?
If you want to integrate js into a dataframe, you may probably want to look at the streamlit-aggrid library. (I haven’t tried this though for your kind of problem)
Another way to do this is:
a. have a dataframe (tdf) of 2 columns (1: your smiles strings…say SString… & 2: image file name… say SmileImg…) which you will pull into st.data_editor
b. the images (which would be pre-saved to disk’s application folder under the smiles string name Eg. CCNC.png
c. the code for the columns can be:
Thanks for the suggestions! I think I can even skip saving files, and encode images as data URI strings ( <img src="data:image/png;base64...), and try to display those using st.column_config.ImageColumn, as you suggested.
I wanted to explore JavaScript options to offload image generation from the server to the browser, and to reduce network traffic of the app. But it sounds like it might be quite a big undertaking, especially if it requires switching to streamlit-aggrid.