Streamlit
is an open resource framework for transforming data scripts into shareable web applications. It allows data scientists and machine learning engineers to build intuitive interfaces without needing to know web development. In this tutorial, we will learn some methods to convert Streamlit application into double-click executable file for Windows/Linux/Mac platforms.
The main motivation behind this is that users should be able to bundle a Streamlit application and all its dependencies into a single package. This bundle can be easily shared with other users who don't have Streamlit or even Python installed in their machines.
Let's create a simple streamer application. Create a file using the following code:streamlit_app.py
import streamlit as st x = st.slider("Select a value") st.write(x, "squared is", x * x)
This application can be run from the terminal using the following command:
streamlit run streamlit_app.py
Let’s look at two frameworks for converting applications into desktop applications: and . Both packages are based on Electron . Electron is a platform that makes it easy to write cross-platform desktop applications using JavaScript, HTML, and CSS. Today, some of the most popular desktop applications are written entirely in Electron, such as Atom, Visual Studio Code, Slack, etc.Nativefier
Stlite
native method
Nativefier is a command line tool that makes it easy to create desktop applications for any website with minimal configuration. Applications are wrapped by Electron in operating system executables (.app, .exe, etc.) for use on Windows, OSX, and Linux. Please note that converting a Streamlit application using Nativefier requires the user to deploy the application to Streamlit Share .
Deploy the application in Streamlit Share and copy the URL of the deployed application as follows:
Install by running the following command:nativefier
npm install -g nativefier
Now convert your Streamlit application like this:
nativefier --name '<app.exe name>' '<streamlit sharing website url>' --platform <'windows' or 'mac' or 'linux'>
This will create the exe file in the current directory.
Package version
Streamlit
1.19.0
Nativefier
50.0.1
Streit method
Stlite is a WebAssembly port of Streamlit that runs on the Pyodide runtime. Pyodide is an experimental project by Mozilla that aims to create a complete Python data science stack that runs entirely in the browser. Streamlit applications can be converted to exe using Stlite desktop . This method does not require deploying the Streamlit application to the Streamlit share.
First create a file to start a new NPM project. You can edit this field.package.json
name
{ "name": "streamlit_app_exe", "version": "0.1.0", "main": "./build/electron/main.js", "scripts": { "dump": "dump-stlite-desktop-artifacts", "serve": 'NODE_ENV="production" electron .', "pack": "electron-builder --dir", "dist": "electron-builder", "postinstall": "electron-builder install-app-deps", }, "build": {"files": ["build/**/*"], "directories": {"buildResources": "assets"}}, "devDependencies": { "@stlite/desktop": "^0.25.0", "electron": "23.1.1", "electron-builder": "^23.6.0", }, }
Install Npm as follows:
npm install
Then create a directory to contain the application files, eg. So the path to the main application file looks like streamlit_app
streamlit_app
/streamlit_app.py
Run the command to create the folder.dump
./build
npm run dump streamlit_app
This command bundles the directory created in the previous step to the application files (.app, .exe, .dmg, etc.) in the ./dist directory.dist
./build
npm run dist
Package version
Streamlit
1.19.0
Stlite desktop
0.25.0
PyInstaller methods
PyInstaller bundles a Python application and all its dependencies into a single package. Users can run packaged applications without installing a Python interpreter or any modules.
Let's see how to bundle a Streamlit application using PyInstaller.
Create wrapper code to run the main application.run.py
import streamlit import streamlit.web.cli as stcli import os, sys def resolve_path(path): resolved_path = os.path.abspath(os.path.join(os.getcwd(), path)) return resolved_path if __name__ == "__main__": sys.argv = [ "streamlit", "run", resolve_path("streamlit_app.py"), "--global.developmentMode=false", ] sys.exit(stcli.main())
Create a hook file:./hooks/hook-streamlit.py
from PyInstaller.utils.hooks import copy_metadata datas = copy_metadata("streamlit")
Now call PyInstaller as follows:
pyinstaller --onefile --additional-hooks-dir=./hooks run.py --clean
This will generate the folder and a file. Edit the file to ensure the path is set correctly as follows:build
dist
run.spec
run.spec
from PyInstaller.utils.hooks import collect_data_files from PyInstaller.utils.hooks import copy_metadata datas = [("{$YOURPYTHONENV}/site-packages/streamlit/runtime", "./streamlit/runtime")] datas += collect_data_files("streamlit") datas += copy_metadata("streamlit") block_cipher = None a = Analysis( ["run.py"], pathex=["."], binaries=[], datas=datas, hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher, noarchive=False, ) pyz = PYZ(...) exe = EXE(...) coll = COLLECT(...)
After running this command, the exe can be found in the path.dist/run.exe
Execute the PyInstaller command again to incorporate the above changes:
pyinstaller run.spec --clean
Make sure to copy to the application path and run again.streamlit_app.py
run.exe
Package version
Streamlit
1.19.0
PyInstaller
5.8.0