Skip to content

Commit

Permalink
Merge pull request DefangLabs#333 from defang-io:Chrisyhjiang-flask
Browse files Browse the repository at this point in the history
Flask Example
  • Loading branch information
raphaeltm authored May 3, 2024
2 parents 90edeeb + c837659 commit 283dffa
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
25 changes: 25 additions & 0 deletions samples/python/flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use an official Python runtime as a base image
FROM python:3.9-slim

# Set the working directory to /app
WORKDIR /app

# Install required packages
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
python3-dev \
&& rm -rf /var/lib/apt/lists/*

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD ["python", "app.py"]
49 changes: 49 additions & 0 deletions samples/python/flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from flask import Flask, jsonify, request, render_template_string

app = Flask(__name__)

# A simple in-memory structure to store tasks
tasks = []

@app.route('/', methods=['GET'])
def home():
# Display existing tasks and a form to add a new task
html = '''
<!DOCTYPE html>
<html>
<head>
<title>Todo List</title>
</head>
<body>
<h1>Todo List</h1>
<form action="/add" method="POST">
<input type="text" name="task" placeholder="Enter a new task">
<input type="submit" value="Add Task">
</form>
<ul>
{% for task in tasks %}
<li>{{ task }} <a href="/delete/{{ loop.index0 }}">x</a></li>
{% endfor %}
</ul>
</body>
</html>
'''
return render_template_string(html, tasks=tasks)

@app.route('/add', methods=['POST'])
def add_task():
# Add a new task from the form data
task = request.form.get('task')
if task:
tasks.append(task)
return home()

@app.route('/delete/<int:index>', methods=['GET'])
def delete_task(index):
# Delete a task based on its index
if index < len(tasks):
tasks.pop(index)
return home()

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
18 changes: 18 additions & 0 deletions samples/python/flask/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.9'
services:
service1:
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
deploy:
resources:
reservations:
cpus: '1.0'
memory: 512M
ports:
- mode: ingress
target: 5000
published: 5000
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5000/"]
4 changes: 4 additions & 0 deletions samples/python/flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Flask==1.1.4
Werkzeug==0.16.1
MarkupSafe==1.1.1

0 comments on commit 283dffa

Please sign in to comment.