Convert your Streamlit application into a desktop app with stlite runtime, a Pyodide-based Wasm-port of Streamlit.
- Create the following
package.json
file to start a new NPM project. Edit thename
field.{ "name": "xxx", "version": "0.1.0", "main": "./build/electron/main.js", "scripts": { "dump": "dump-stlite-desktop-artifacts", "serve": "cross-env 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.39.0", "cross-env": "^7.0.3", "electron": "^26.2.4", "electron-builder": "^24.6.4" } }
- Run
npm install
oryarn install
. - Create
streamlit_app
directory.- Any directory name can be used here.
- Create
streamlit_app/streamlit_app.py
.- Change the directory name if you used a different name in the previous step.
- Write your Streamlit app code in
streamlit_app/streamlit_app.py
.- The file name
streamlit_app.py
is not configurable now.
- The file name
- Optionally, you can add more contents in the directory, including
pages/*.py
for multi-page apps, any data files, and so on. - Run
npm run dump streamlit_app
oryarn dump streamlit_app
. The command argumentstreamlit_app
is the directory name of the Streamlit app you have created in the previous steps. Change it if you used a different name.- If installing some packages is needed, pass the package names following the directory name like
npm run dump streamlit_app <PackageName1> ... <PackageNameN>
. - The
-r
option like thepip
command is also available to specify the text files listing the package names to install likenpm run dump streamlit_app -- -r requirements.txt
(NPM) oryarn dump streamlit_app -r requirements.txt
(Yarn). Note that if you are using NPM, you need to add--
before options such as-r
in therun
command (ref). - This
dump
command creates./build
directory. It includes- The stlite bare app files.
streamlit_app
directory copied from the one you created in the previous steps.site-packages-snapshot.tar.gz
that includes the installed package files.
- If installing some packages is needed, pass the package names following the directory name like
- Run
npm run serve
oryarn serve
for preview.- This command is just a wrapper of
electron
command as you can see at the"scripts"
field in thepackage.json
. It launches Electron and starts the app with./build/electron/main.js
, which is specified at the"main"
field in thepackage.json
.
- This command is just a wrapper of
- Run
npm run dist
oryarn dist
for packaging.- This command bundles the
./build
directory created in the step above into application files (.app
,.exe
,.dmg
etc.) in the./dist
directory. To customize the built app, e.g. setting the icon, follow theelectron-builder
instructions.
- This command bundles the
To make your app secure, be sure to use the latest version of Electron. This is announced as one of the security best practices in the Electron document too.
If you want to hide the toolbar, hamburger menu, and the footer, add the following to your package.json
file and run the dump
command again. By adding the stlite.desktop.embed
field, the dumped Streamlit app will work in the embed mode which hides the toolbar, hamburger menu, and the footer.
{
// ...other fields...
"stlite": {
"desktop": {
"embed": true
}
}
}
- Navigation to external resources like
st.markdown("[link](https://streamlit.io/)")
does not work for security. See whitphx#445 and let us know if you have use cases where you have to use such external links.