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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.