Will Streamlit.io play nicely with Anvil?

Hi everybody,

I’ve been working with a new pure-python data science platform called Streamlit.io to build some quick data profiling tools for a client, it’s a pretty cool prototyping environment!

I’m keen to see if it will play nicely with Anvil so I can embed some of the functionality into an Anvil app and manage custom domains/user access/tabbed views etc.

Any thoughts? I think it would be a great application…

I think they will play nicely depending on exactly what you are doing.

Could you give a specific example of what you had in mind or what you have tried?

If you are embedding a Streamlit app inside an Anvil app, you could try an iframe. Otherwise perhaps you could research the following documentation if you have not already: Anvil uplink, Jupyter + Anvil tutorial, HTTP requests, HTTP endpoints.

1 Like

Thanks for this, I’ve just taken some time to build a really basic streamlit app which I want to try and embed in Anvil (to take advantage of the user management etc. features). The approach to building a streamlit app is:

  1. Install Streamlit (in a virtual environment): pip install streamlit
  2. Write the app in pure Python (see below for anvil_streamlit_demo.py code)
  3. Run app using streamlit run anvil_streamlit_demo.py
  4. Browse to http://localhost:8501 to interact with running app

I’m not sure where to start with embedding this in Anvil, or even where the app should be running… also it would be great if I could pass variables (e.g. user_email, user_firstname) back to Streamlit to display in the app.

Code for the demo app (anvil_streamlit_demo) is below, it’s a kind of pointless standalone app but shows a bit of user interactivity via data filtering from the multi-select box and data/chart display of the filtered data. I know that most of this could be reproduced in Anvil, but the workflow for building in streamlit is really clean and quick and there’s a ton of functionality out of the box which I find really useful and powerful.

Any pointers would be very gratefully received, thanks!

    import streamlit as st
    import pandas as pd
    import altair as alt
    
    # dummy user info
    user_email = "whoever@isloggedin.com"
    user_firstname = "Zaphod"
    
    st.markdown("# Anvil/Streamlit Demo")
    st.markdown(f"### Hello {user_firstname}, you are logged into the demo")

    # set up data
    demo_data = [['WED', 2400], ['THU', 3700], ['FRI', 4000], ['SAT', 4100], ['SUN', 4250]]
    demo_dataframe = pd.DataFrame(demo_data, columns=['day', 'max_freezing_level'])
    
    # add elements to sidebar and filter out days based on user multi-select
    st.sidebar.markdown(f"User: {user_email}")
    st.sidebar.markdown("# Options: ")
    excluded_days = st.sidebar.multiselect("Select Days to Exclude:", demo_dataframe['day'])
    
    demo_dataframe_filtered = 
    demo_dataframe[~demo_dataframe.day.isin(excluded_days)].reset_index(drop=True)

    # display dataframe
    st.markdown("## Interactive Dataframe")
    st.write(demo_dataframe_filtered)
    
    # display bar chart
    st.markdown("## Example Altair Chart")
    chart_altair = alt.Chart(demo_dataframe_filtered).mark_bar().encode(
       x=alt.X('day', type='nominal', sort=None),
    y='max_freezing_level:Q',
    tooltip=['day', 'max_freezing_level']).interactive()
    st.altair_chart(chart_altair)

Not sure if it’s helpful, but a Streamlit co-founder briefly describes how to deploy as a web app generally here: https://news.ycombinator.com/item?id=21169078

Theoretically, the server module could run the other server on a specified port, then connect the output to display as an iframe using something like <iframe src="http://localhost:8501"></iframe>. Streamlit isn’t currently available as a server package though.

Then using Anvil buttons or selectors to connect back to Streamlit would be theoretically possible. I would question the purpose of this though. The only thing you would gain would be a nice material navigation around the app page.

Is there currently functionality of Streamlit you like that isn’t available in Anvil? For me personally I moved to Anvil from Dash because it was much faster to develop but still performant and scaleable easily (with the different plans).

1 Like

Fair enough! Streamlit is pretty nice for rapid prototyping but since it can’t be straightforwardly shared or deployed I’ve decided to use Anvil instead!

3 Likes