forked from DefangLabs/defang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DefangLabs#333 from defang-io:Chrisyhjiang-flask
Flask Example
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|