Turn streamlit application into a pyinstaller executable
Asked Answered
C

2

7

I am building an application in streamlit that, for several reasons, cannot be hosted on a server. The most blocking reason is that it should also work without internet access. Therefore, I would like to turn my application in some sort of executable that users can run on their own laptop and the app is hosted on localhost.

I have the following code:

import streamlit.cli as stcli
import sys

def streamlit_run():
    sys.argv = ["streamlit", "run", "main.py", "--global.developmentMode=false"]
    sys.exit(stcli.main())


if __name__ == '__main__':
    streamlit_run()

main.py is for now a simple hello-world application. If I run

python wrapper.py

in my command line, the app runs on localhost. I turn it into an executable using pyinstaller, running

pyinstaller --onefile script.py

and an executable file is created. However, if I run this executable, nothing happens: I only see a black screen for a split second and that's it. Any help on how I can resolve this issue? Any other solution to let cleints use the application without hosting it on a server would be appreciated as well!

Versions: python==3.9 streamlit==0.73.0 pyinstaller==4.1

Closing answered 18/12, 2020 at 12:39 Comment(1)
Have a look at discuss.streamlit.io/t/…Mackoff
W
0

First, try runnning it from the terminal instead of double clicking the file -> Assuming your file is stored in the downloads folder,

cd C:\Downloads
script.exe

If that doesn't work, it is likely because your .exe file is not signed and is perceived as a threat by windows defender and removed.

A better way of letting others use your streamlit app would be through uploading your project to github and providing detailed instructions on how to clone and run the project, ending with:

streamlit run main.py
Willard answered 19/6 at 7:42 Comment(0)
S
0

Bit late to the party, but I got it working well using auto-py-to-exe following the steps below.

  1. pip install auto-py-to-exe.
  2. For your streamlit application, if you haven't already, write a new script, called execute_app.py, that calls your app.py, like below.

import os
import sys
from streamlit.web import cli as stcli

if __name__ == "__main__":
    os.chdir(os.path.dirname(__file__))

    sys.argv = ["streamlit", "run", "app.py", "--server.port=8501", "--global.developmentMode=false", \
                "--theme.base=light", "--theme.primaryColor=#10475a", "--theme.secondaryBackgroundColor=#f2f9f2"]

    stcli.main()
  1. Call auto-py-to-exe by typing auto-py-to-exe in the terminal to load the graphic interface.
  2. Under Script Location, select the location of execute_app.py.
  3. Under Onefile select One Directory. I found that One File takes a ridiculously long time to unpack, especially if your streamlit app is large like mine. It's much faster to use --onedir.
  4. If you don't want the console to appear, select Window Based under Console Window. However, for your first attempt at converting to .exe, I recommend you select Console Based since you will receive error messages this way if you need to debug.
  5. If you want your .exe file to have an icon, specify the path under Icon.
  6. Under Additional Files add any folders and any scripts that appear in the same directory as your app.py file.
  7. Under Advanced give your .exe file a name.
  8. Now comes the important steps. Under hidden-imports, collect-all, and copy-metadata include the names of all of the libraries that you have imported. Note that specifying all of the libraries is probably overkill, but I just took a crude go-all-in approach to make sure I got everything.

Then hit CONVERT .PY TO .EXE and hopefully you'll have a working local streamlit executable you can share with your colleagues!

Seasoning answered 26/9 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.