Skip to content

Commit ab59493

Browse files
committed
Flask-railpack sample
1 parent 2ee39f9 commit ab59493

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]

samples/flask-railpack/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Flask
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-flask-template%26template_owner%3DDefangSamples)
4+
5+
This is a basic Flask to-do app that can be deployed with Defang. Note that alongside your `.py` file, include a `requirements.txt` so that the Dockerfile can install the necessary packages with pip.
6+
7+
This project is intended to provide a basic understanding of how to get started with Flask on Defang. The items are stored in memory and are lost when the server is restarted. **It is not intended for production use**. If you need something production ready, you should use a managed database like Postgres or MySQL.
8+
9+
## Prerequisites
10+
11+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
12+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
13+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
14+
15+
## Development
16+
17+
To run the application locally, you can use the following command:
18+
19+
```bash
20+
docker compose up --build
21+
```
22+
23+
## Configuration
24+
25+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
26+
27+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
28+
29+
```bash
30+
defang config set API_KEY
31+
```
32+
33+
## Deployment
34+
35+
> [!NOTE]
36+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
37+
38+
### Defang Playground
39+
40+
Deploy your application to the Defang Playground by opening up your terminal and typing:
41+
```bash
42+
defang compose up
43+
```
44+
45+
### BYOC (AWS)
46+
47+
If you want to deploy to your own cloud account, you can use Defang BYOC:
48+
49+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
50+
2. Run in a terminal that has access to your AWS environment variables:
51+
```bash
52+
defang --provider=aws compose up
53+
```
54+
55+
---
56+
57+
Title: Flask
58+
59+
Short Description: A basic Flask to-do app.
60+
61+
Tags: Flask, Python
62+
63+
Languages: python
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
flask:
3+
restart: unless-stopped
4+
build:
5+
context: ./flask
6+
deploy:
7+
resources:
8+
reservations:
9+
cpus: "1.0"
10+
memory: 512M
11+
ports:
12+
- mode: ingress
13+
target: 5000
14+
published: 5000
15+
healthcheck:
16+
test: ["CMD", "python3", "-c", "import sys, urllib.request; urllib.request.urlopen(sys.argv[1]).read()", "http://localhost:5000/"]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from flask import Flask, jsonify, request, render_template_string
2+
3+
app = Flask(__name__)
4+
5+
# A simple in-memory structure to store tasks
6+
tasks = []
7+
8+
@app.route('/', methods=['GET'])
9+
def home():
10+
# Display existing tasks and a form to add a new task
11+
html = '''
12+
<!DOCTYPE html>
13+
<html>
14+
<head>
15+
<title>Todo List</title>
16+
</head>
17+
<body>
18+
<h1>Todo List</h1>
19+
<form action="/add" method="POST">
20+
<input type="text" name="task" placeholder="Enter a new task">
21+
<input type="submit" value="Add Task">
22+
</form>
23+
<ul>
24+
{% for task in tasks %}
25+
<li>{{ task }} <a href="/delete/{{ loop.index0 }}">x</a></li>
26+
{% endfor %}
27+
</ul>
28+
</body>
29+
</html>
30+
'''
31+
return render_template_string(html, tasks=tasks)
32+
33+
@app.route('/add', methods=['POST'])
34+
def add_task():
35+
# Add a new task from the form data
36+
task = request.form.get('task')
37+
if task:
38+
tasks.append(task)
39+
return home()
40+
41+
@app.route('/delete/<int:index>', methods=['GET'])
42+
def delete_task(index):
43+
# Delete a task based on its index
44+
if index < len(tasks):
45+
tasks.pop(index)
46+
return home()
47+
48+
if __name__ == '__main__':
49+
app.run(debug=True, host='0.0.0.0')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==2.2.5
2+
Werkzeug==2.2.2
3+
MarkupSafe==2.1.1

0 commit comments

Comments
 (0)