Cannot properly deploy shiny python app with environment variables to Shinyapps.io

Hello,

I'm having problems posting a shiny for python app that makes use of environment variables passed in using the rsconnect CLI following the help documentation. The deployment concludes with no errors, but when I view the app it says "The application failed to start", and I can see from the logs that none of the environment variables made it to shinyapps.io. Is this possible to do? If so, can someone help me figure out if I'm doing something wrong?

I have a Shiny for Python app that I'm trying to deploy to my personal (free tier) shinyapps.io account that tracks my music practice sessions so I can share them with my teacher. The app connects to a postgres database hosted on the cloud and I store the credential information in virtual environment environment variables (pg_schema, pg_host, pg_user, etc). Locally this works fine and my app uses code like os.environ['pg_schema'] to access the environment variables.

For my deployment step I first create my own requirements.txt file and use that to write a manifest.json. Then I use the following command to deploy:

rsconnect deploy shiny --new data_entry_app/ --name shinyapps-io --title "test_db_app_pg" -E pg_user=$pg_user -E pg_pw=$pg_pw -E pg_host=$pg_host -E pg_port=$pg_port -E pg_dbname=$pg_dbname -E pg_schema=$pg_schema

I've also tried this by removing the assignment of each env var in the rsconnect deploy step (-E pg_user -E pg_pw -E pg_host etc...) but the result is the same.

I appreciate any guidance from the community on this. Thanks!

Providing a response to my own question as I was able to get some help off the Shiny Discord channel. It appears that as the time of this post, Shinyapps.io doesn't support rsconnect's -E/--environment argument to push environment variables the way Posit Connect does. However, an alternate method that works just fine is to create a file with a .env extension in the same folder as the shiny app and add your environment variables there. MAKE SURE you add this file .env to your .gitignore file if you are source tracking your project in a git repo to avoid storing the credentials there.

The env file (in this example I'll call it variables.env) can have something like:

pg_user='*****'
pg_pw='*****'
pg_host='my.data.base'
pg_port='5432'
pg_dbname='my_db'
pg_schema='my_schema'

Then in your source code when you need those values in memory, use the dotenv package (pip install python-dotenv) to pull them in:

import os
from dotenv import load_dotenv
load_dotenv("variables.env")

user=os.getenv("pg_user")
password=os.getenv("pg_pw")
host=os.getenv("pg_host")
port=os.getenv("pg_port")
dbname=os.getenv("pg_dbname")
schema=os.getenv("pg_schema")

This should deploy to shinyapps.io using a requirements.txt file, mainfest.json file and rsconnect syntax similar to:

rsconnect deploy shiny --new data_entry_app/ --name shinyapps-io --title "test_db_app"

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.