mkdir -p environment-variables-tutorial
cd environment-variables-tutorial
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.
You can use this pattern in any of your Plash apps. Simply:
plash.env
file in your project rootexport KEY=VALUE
formatos.environ['KEY']
)Plash automatically sets environment variable PLASH_PRODUCTION=1
, which you can use to detect whether your app is in production or development.
In this tutorial, you’ll learn how to use environment variables in your Plash app.
Prerequisites:
pip install plash-cli
if needed)plash_login
if needed)First, create a directory for your project named environment-variables-tutorial
and navigate into it.
Then, create a file named plash.env
in your project directory with the following content:
Now, create a main.py
file for your FastHTML application:
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:
So far you’ve created the following files:
Now you’re ready to deploy your app. Run the following cli command:
Follow the displayed link. You will see:
🎉 Complete: Environment Variables Tutorial