How to Deploy a Python Flask App on Railway
A brief guide to deploying a Python Flask app on Railway.
In this post, we'll walk through the steps to deploy a Python Flask app on Railway.
What is Flask?
Flask is a lightweight web application framework written in Python. It is based on the Werkzeug WSGI toolkit and the Jinja2 template engine, and is one of the most popular Python frameworks around. It does not require specific tools or libraries, but supports a wide range of extensions for building web applications.
What is Railway?
Railway is a modern app hosting platform that makes it easy to deploy production-ready apps quickly. Railway offers persistent database services for PostgreSQL, MySQL, MongoDB, and Redis, as well as application services with a GitHub repository as the deployment source. For the latter, Railway can automatically determine the application runtime and deploy the service. Railway offers several one-click starters for popular applications, including Flask. Since we are just testing the waters, Railway's free tier should be sufficient to host the service.
Deploy a Simple Flask App on Railway
Sign up for an account with Railway using GitHub, and click Authorize Railway App
when redirected. Review and agree to Railway's Terms of Service and Fair Use Policy if prompted. Launch the Flask one-click starter template (or click the button below) to deploy Flask instantly on Railway.
You'll be given an opportunity to change the default repository name and set it private, if you'd like. Accept the defaults and click Deploy
; the deployment will kick off immediately.
Once the deployment completes, a simple Flask app will be available at a default xxx.up.railway.app
domain - launch this URL to access the app. If you are interested in setting up a custom domain, I covered it at length in a previous post - see the final section here.
If you want to modify the app, here's a few things to understand first:
- The
main.py
file only includes the default/
route, which launches theindex.html
file under thetemplates
folder. Add the routes you need, along with the corresponding template files. - If you'd like to serve static files ( e.g.
.css
,.js
or images), add astatic
folder to the root, with the correspondingcss
,js
andimg
sub-folders to host your files. Then reference them accordingly in your template files. - A
Procfile
has been added to the root - this tells Railway how to run the app.
In conclusion, deploying a Python Flask app on Railway is a straightforward process, made even easier by the one-click template. Go forth and publish!