avatarAlan Jones

Summary

The website content introduces "Mustrapp," a Streamlit application template designed to simplify the creation of a web app that can host and run multiple Streamlit apps from a single gallery page.

Abstract

The provided content discusses the challenge of presenting multiple Streamlit apps through a single web interface. The author, Alan Jones, presents a solution called "Mustrapp," which is a Streamlit application template that allows developers to organize their apps into a library and use a dispatcher to select and run them. This template requires each app to have a run() function and an optional description string for the dropdown menu in the gallery interface. The structure allows for scalability and easy maintenance of multiple apps within a single Streamlit deployment. The article also includes practical examples, links to a GitHub repository with the complete template, and instructions on how to set up and run the multi-app gallery.

Opinions

  • The author expresses that individual Streamlit apps may not warrant a dedicated web page and thus advocates for a gallery approach.
  • Using an if statement to switch between apps within a single Streamlit app is deemed not scalable by the author.
  • The "Mustrapp" template is touted as a simple and effective framework for managing multiple Streamlit apps, emphasizing its ease of use and maintainability.
  • The author believes that running apps as stand-alone functions is useful for debugging purposes.
  • The dispatcher system is described as working "quite well," indicating the author's satisfaction with the solution.
  • The use of a drop-down menu for app selection, populated with either app names or descriptions, is presented as a user-friendly feature of the template.
  • The author humbly acknowledges that the solution is not "rocket science" but still hopes that readers find it useful.
  • The article concludes with an invitation for readers to sign up for alerts on new articles and a plug for the author's LinkedIn and Twitter profiles, suggesting a desire to build a community and following around their work.

Data Visualization

How to Build a Gallery of Streamlit Apps as a Single Web App

Download a free template, pop in multiple Streamlit apps and you’re done

Image by author

I’ve been playing around with Streamlit for a little while now and have written a handful of experimental apps. While I’m happy enough with them (as far as they go — they are only simple examples), none of them are worthy of a dedicated web page.

So what is the best way of presenting a gallery of apps that can be accessed from a single web page?

You could put all the code in a single app and chose between them with an if statement (as I did in this article) but that’s not very scalable — once you get to more than a couple of apps it gets messy.

So how about creating a library of apps with a dispatcher that calls each one depending of the selection in a drop-down menu. That way the apps are written as stand-alone functions. You just create a library folder to contain the apps and the dispatcher calls the appropriate one.

That sounds better, I think.

Mustrapp a simple framework

So, to that end, I have created an application template called, Mustrapp (Multiple Streamlit Apps), (that you can download for free) which makes the process of creating a multiple Streamlit app very easy.

For the apps to work you need to structure them so that they are in a callable function named run(). And, ideally (but not necessarily), contain a description string that will be used as the text in the drop down menu (if you don’t provide one the module name will be used).

So, an app will look like this:

description = "My first app"
def run():
   st.header("This is my first app")
   st.write("I hope you like it")
if __name__ == "__main__":
   run()

You can have as many functions as you like in an app but it must start in a function called run and that’s about the only requirement. The last if statement is optional but useful as it allows you to run the individual app as a stand-alone app, or as part of the multi-app. It basically checks to see is the function has been called as a main module (in which case its __name__ will be __main__). If it is called as a library module (which is what we intend to do) then __name__ will be the module name.

Being able to run the app as a stand-alone function can be useful for debugging purposes as you don’t need the rest of the application to be operational.

Let’s say you write the app above and save it in a file app1.py.

You could then write another app, app2.py, that looks like this:

description = "My second app"
def run():
   st.header("This is my second app")
   st.write("Here is an image:")
   st.image("image.png")
if __name__ == "__main__":
   run()

We then put these files in a folder called, for example, stlib — this will be our library folder where all our apps will live. In order for Python to recognise this folder as a library it needs to contain a file called __init__.py. If you’ve downloaded the template it is already there.

The dispatcher lives in the home folder. This is the program that will be run when we start up the Streamlit app. I’m going to call it index.py.

So our complete app is structured like this:

/home
 |
 |-- index.py
 |
 |-- /stlib
       |
       |-- __init__.py
       |
       |-- app1.py
       |
       |-- app2.py

To run the app you execute this:

streamlit run index.py

And this will pop up in your browser:

Example app — image by author

You can see the full code of the dispatcher in a Gist at the very end of this article (it’s too long to include here). And I will also include, at the end, a link to a Github repository where you can clone, or download a zip file of, the complete template including the dispatcher, the directory structure and a couple of dummy apps.

But basically it works like this.

First we import the Streamlit library (of course) along with the other libraries that we need. (It also sets the layout to wide. This is optional but to my mind it looks better if you are going to use a sidebar, as we are doing here — this can be removed if you prefer.)

We then set about identifying the apps in the library (stlib) and record the names, module references and descriptions of the apps in global arrays called names, modules and descriptions. Any module in the stlib library is deemed to be a Streamlit app unless its name begins with the _ character. In this way you can add non-app library modules (e.g. _mylibrary.py), if you need to and they won’t be picked up as Streamlit apps.

Having done this, we create a drop-down menu populated with the module names (the descriptions are displayed, if available) and once selected, the appropriate module is selected from the modules array and its function run() is executed. In other words the app that corresponds to the select is run.

Even if I say it myself, this works quite well. Using the dispatcher app with an arbitrary number of callable apps is easy and it is very straightforward to maintain. Now we can deploy any number of mini-apps in one go, on one web page.

None of this is rocket science but I hope it has been useful. You can see a sample website that uses this template here and you can download, or clone, the complete template from its Github repository.

As ever thanks for reading and if you would like to know when I publish new articles, please consider signing up for an email alert here. I also publish an occasional free newsletter on Substack. Please leave any comments below or you can get in touch via LinkedIn or Twitter.

Here, for interest, is the dispatcher code:

Streamlit
Tutorial
Data Visualization
Python
Programming
Recommended from ReadMedium