Add Environment Variables
Use environment variables to configure your app without changing code. Perfect for secrets, API keys, and other configuration values.
What are environment variables in plash
Environment variables allow you to store configuration and sensitive information outside your codebase, making your application more secure and flexible across different environments.
Plash provides a simple way to set and use environment variables in your deployed applications.
Using environment variables in your own apps
You can use this pattern in any of your Plash apps. Simply:
- Create a
plash.envfile in your project root - Add your environment variables using
export KEY=VALUEformat - Access them in your app (e.g. using
os.environ['KEY'])
Plash automatically sets environment variable PLASH_PRODUCTION=1, which you can use to detect whether your app is in production or development.
Step by step tutorial
In this tutorial, you'll learn how to use environment variables in your Plash app.
Prerequisites:
- A registered account at https://pla.sh
- The Plash CLI installed (run
pip install plash-cliif needed) - Logged in via the CLI (run
plash-loginif needed)
1. Create your environment variable file
First, create a directory for your project named environment-variables-tutorial and navigate into it.
%%bash
mkdir -p environment-variables-tutorial
cd environment-variables-tutorial
Then, create a file named plash.env in your project directory with the following content:
%%writefile plash.env
export APP_NAME="My Environment Variables Demo"
2. Create your FastHTML app
Now, create a main.py file for your FastHTML application:
%%writefile main.py
from fasthtml.common import *
import os
app, rt = fast_app()
@rt
def index():
# Access environment variables using os.environ
app_name = os.environ.get('APP_NAME', 'Default App Name')
environment = "Production" if os.environ.get('PLASH_PRODUCTION', None) else "Development"
return Div(
H1(app_name),
P(f"Running in: {environment}"),
style="font-family: system-ui; max-width: 600px; margin: 0 auto; padding: 2rem;"
)
serve()
Also create a requirements.txt file:
%%writefile requirements.txt
python-fasthtml
Deploy your app
So far you've created the following files:
- plash.env
- main.py
- requirements.txt
Now you're ready to deploy your app. Run the following cli command:
%%bash
plash-deploy
Follow the displayed link. You will see:
- The APP_NAME environment variable as the heading
- A message describing the app is running in production
🎉 Complete: Environment Variables Tutorial