Packaging and Deployment with Shiv

I have managed to setup a project template which allows me to test my Streamlit app in PyCharm IDE on Windows 11 and package it up using Shiv shiv :hocho: β€” shiv documentation so I can deploy it on a VM.

This is the directory structure of my repo…

.
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ 00-Login.py
β”‚   β”œβ”€β”€ credentials.yaml
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ __init__.py
β”œβ”€β”€ .streamlit
β”‚   β”œβ”€β”€ config.toml
β”‚   β”œβ”€β”€ login.css
β”‚   β”œβ”€β”€ secrets.toml
β”‚   β”œβ”€β”€ style.css      
β”œβ”€β”€ images
β”‚   β”œβ”€β”€ icon.ico
β”‚   β”œβ”€β”€ logo.png     
β”œβ”€β”€ components
β”‚   β”œβ”€β”€ common.py
β”‚   β”œβ”€β”€ services.py
β”‚   β”œβ”€β”€ __init__.py          
β”œβ”€β”€ pages
β”‚   β”œβ”€β”€ 01-Chat.py
β”‚   β”œβ”€β”€ 99-About.py
β”‚   β”œβ”€β”€ __init__.py
β”œβ”€β”€ DEPLOYMENT.md
β”œβ”€β”€ MANIFEST.in
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
└── setup.py  

I use Shiv to package up into a .pyz file as follows:

shiv --compressed -p '/usr/bin/env python3' -o app.pyz -e app.main:main
 . -r requirements.txt

I then move to the VM and launch using:

$ python app.pyz

Here is what main,py looks like that is defined as the endpoint and launches the streamlit app:

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

def main():
    os.chdir(Path(__file__).parent)
    cwd = os.getcwd()
    sys.path.append(cwd)

    streamlit_initfile = os.path.join('00-Login.py')
    sys.argv = ["streamlit", "run", streamlit_initfile]
    sys.exit(stcli.main())

if __name__ == "__main__":
    main()

To ensure the app can be run locally in my IDE and in the VM I dynamically set the working directory in each page as follows and this works fine for referencing images and resources within the venv regardless of where it is running from:

import os
from pathlib import Path
os.chdir(Path(__file__).parent)
cwd = os.getcwd()

The app runs perfectly locally in the IDE and remotely on the VM except, when running on the VM, the app cannot locate the secrets.toml file!

It seems streamlit must be using a different mechanism to locate the config.toml than the secrets.toml file. If this is the case, could it be a potential defect in the streamlit package? Anyone with experience of this type of issue?

VERSIONS

  • python 3.11.4
  • streamlit v1.24.1
  • shiv 1.0.3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.