How to Convert a Streamlit App to an .EXE Executable

3 min read 1 year ago
Published on Aug 07, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you'll learn how to convert a Streamlit app into an executable (.EXE) file using a streamlined process involving WebAssembly and Electron. This method allows you to share your Streamlit applications easily without requiring your users to install Python locally. The process is straightforward, consisting of just three commands.

Step 1: Set Up Your Environment

Before you start, ensure you have the following installed on your machine:

  • Node.js: This is required for Electron.
  • Python: Make sure you have Python installed, as you'll be using Streamlit.

Practical Tips

  • Check if Node.js is installed by running node -v in your terminal.
  • For Python, use python --version to confirm its installation.

Step 2: Install Required Packages

You will need to install specific packages to facilitate the conversion process. Open your terminal and run the following commands:

  1. Install Electron:

    npm install -g electron
    
  2. Install Stlite: Stlite allows Streamlit to run in the browser context.

    pip install stlite
    

Common Pitfalls

  • Ensure you're running these commands in the correct environment (e.g., virtual environment for Python).
  • If you encounter permission issues, consider using sudo for the installation commands.

Step 3: Create Your Streamlit App

If you haven't already created a Streamlit app, do so now. Here’s a simple example of a Streamlit app:

import streamlit as st

st.title("My First Streamlit App")
st.write("Hello, world!")

Save this code in a file named app.py.

Step 4: Build the Executable

Now that you have your app ready, you can convert it into an executable. Run the following command in your terminal:

stlite build app.py

Additional Information

  • The build command compiles your Streamlit app and prepares it for distribution.
  • Make sure to replace app.py with the name of your Streamlit file if it differs.

Step 5: Run Your Executable

Once the build process is complete, you can find your .EXE file in the output directory. You can run it by navigating to that directory and executing the file directly.

Practical Tips

  • Test the executable on different machines to ensure compatibility.
  • Share it with friends or family to demonstrate its functionality.

Conclusion

Converting a Streamlit app to an .EXE file is a quick and efficient way to distribute your applications. By following these steps, you can create executable files that allow users to run your apps without needing Python installed. This method, utilizing Electron and Stlite, makes sharing your projects seamless.

Next steps could include exploring more complex Streamlit features or enhancing your app's functionality before sharing it further.