requirements.txt
and a production command (gunicorn
).Imagine your current script calculates a greeting:
# logic.py
def make_greeting(name: str) -> str:
return f"Hello, {name or 'world'}!"
What this does: Keeps your core logic in a pure function so it’s easy to test and reuse (CLI, web, cron jobs, etc.).
# app.py
from flask import Flask, request, jsonify, render_template_string
from logic import make_greeting
import os
app = Flask(__name__)
# --- HTML page (GET /) ---
@app.get("/")
def index():
name = request.args.get("name", "")
msg = make_greeting(name)
# tiny inline template for demo
html = """
<!doctype html>
<title>Flask Greeting</title>
<h1>{{ msg }}</h1>
<form method="get" action="/">
<input name="name" placeholder="Your name">
<button type="submit">Greet</button>
</form>
<p>Try the JSON API at <code>/api/greet?name=Alice</code></p>
"""
return render_template_string(html, msg=msg)
# --- JSON API (GET /api/greet) ---
@app.get("/api/greet")
def api_greet():
name = request.args.get("name", "")
return jsonify({"greeting": make_greeting(name)})
# Local run helper (platforms set PORT)
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=True)
What’s happening:
index()
reads ?name=
from the URL, calls your function, and renders a tiny HTML page.api_greet()
exposes the same logic as JSON for programmatic use.0.0.0.0
and using PORT
from the environment makes it compatible with hosts like Render/Railway. (Flask quickstart basics are here. flask.palletsprojects.com)Create a requirements.txt
:
flask>=3.0
gunicorn>=21.2
Why gunicorn
? Production platforms expect a WSGI server to run Flask apps (not flask run
). We’ll start it like gunicorn app:app
. (Gunicorn + Flask deployment background. Red Hat Developer)
# optional but recommended
python -m venv venv
# Windows: venv\Scripts\activate
source venv/bin/activate
pip install -r requirements.txt
python app.py
# Visit http://localhost:5000
Create a repo with at least:
app.py
logic.py
requirements.txt
(Add .gitignore
with venv/
if you used one.)
One-time setup (via dashboard):
requirements.txt
.pip install -r requirements.txt
gunicorn app:app
Render’s official Flask quickstart mirrors these steps. If you pick their example first, you’ll see the same pattern. Render+1testdriven.io
Tip: If you set the Start Command to
python app.py
, Render may complain (no open HTTP port) or run with the wrong server. Use Gunicorn for web services. Render
Option A — One-click template / guide: Railway provides a Flask guide and templates—pick Flask, connect GitHub, and Railway auto-detects Python with requirements.txt
. Railway DocsRailway
Option B — From your GitHub repo:
requirements.txt
.gunicorn app:app
.SECRET_KEY
). RedditWhy this works: Railway detects Python automatically, provisions a web service, and sets a PORT
environment variable your app binds to. Railway Docs
After deploy:
https://<your-app>/
(page)https://<your-app>/api/greet?name=Alice
(JSON)flask run
on hostsgunicorn app:app
for production web services. Red Hat Developerapp.run(host="0.0.0.0", port=int(os.getenv("PORT", 5000)))
.portaudio
for PyAudio). Prefer alternatives or deploy in a Docker container if needed. RedditIf your host uses Procfiles (Railway supports Docker/CLI/templates and may not require it), you can include:
web: gunicorn app:app
This is a conventional way to declare your web process; many PaaS recognize it.
templates/
and use render_template
.static/
.