Skip to content

Commit

Permalink
Merge pull request #14 from tainguyenbp/feat/learing-python-language
Browse files Browse the repository at this point in the history
learning python language
  • Loading branch information
tainguyenbp authored Jul 6, 2024
2 parents e465ed7 + 2762a3b commit 90c485e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
35 changes: 35 additions & 0 deletions python-basic/application-flask-lib/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# FROM python:3.9-slim

# WORKDIR /app

# COPY requirements.txt requirements.txt
# RUN pip3 install -r requirements.txt

# COPY . .

# CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]


# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install any needed packages specified in requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

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

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

# Define environment variable
ENV FLASK_ENV=production

# Run app.py when the container launches
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]
23 changes: 23 additions & 0 deletions python-basic/application-flask-lib/application-flask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import Flask
from twilio.rest import Client
app = Flask(__name__)
@app.route('/trigger-calls', methods=['GET', 'POST'])
def trigger_call():
account_sid = '<acc-sid>'
auth_token = '<acc-auth>'
client = Client(account_sid, auth_token)
# List of numbers to call
numbers_to_call = ['<country-code-number>', '<country-code-number>']
call_sid = [] # To store Call SIDs
for number in numbers_to_call:
call = client.calls.create(
twim='<Response><Say>"change Alert message here"</Say></Response>',
to=number,
from_='<twilio-number>'
)
call_sids.append(call.sid)
# Joining all the Call SIDs to return in response
call_sids_str = ', 'join(call_sids)
return f"Call initiated with SIDs: {call_sids_str}", 200
if __name__ == '__main__':
app.run(host'0.0.0.0', port=5000)
13 changes: 13 additions & 0 deletions python-basic/application-flask-lib/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### test
```
sudo apt install python3-pip
sudo apt install python3.12-venv
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
docker build -t webhook .
docker run -d --name webhook -p 5000:5000 webhook:v1.0.0
```
3 changes: 3 additions & 0 deletions python-basic/application-flask-lib/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==2.0.1
twilio==7.4.0
Werkzeug==2.0.1
25 changes: 25 additions & 0 deletions python-basic/gather-data-api/gather-data-api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/python3

import requests
import sys


if __name__ == "__main__":
# Define the URL for the REST API
url = "https://jsonplaceholder.typicode.com/"

# send a GET request to retrieve user info
user = requests.get(url + "users/{}".format(sys.argv[1])).json()

# send a GET request to retrive the TODO list
todos = requests.get(url + "todos", params={"userId": sys.argv[1]}).json()

# filter completed TODO list and store titles in a list
completed = [t.get("title") for t in todos if t.get("completed") is True]

# print employee's name, completed tasks & total no of tasks
print("Employee {} is done with tasks({}/{}):".format(
user.get("name"), len(completed), len(todos)))

# print the titles of completed tasks with indentation
[print("\t {}".format(c)) for c in completed]
11 changes: 11 additions & 0 deletions python-basic/gather-data-api/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### test
```
sudo apt install python3-pip
sudo apt install python3.12-venv
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python3 gather-data-api .py
```

0 comments on commit 90c485e

Please sign in to comment.