SanjeevB1 commited on
Commit
18a7173
·
verified ·
1 Parent(s): 97d5e9e

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +17 -0
  2. app.py +80 -0
  3. data.db +0 -0
  4. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.9 image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory
5
+ WORKDIR /app
6
+
7
+ # Copy files to the container
8
+ COPY . .
9
+
10
+ # Install dependencies
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Expose port 7860 for Hugging Face Spaces
14
+ EXPOSE 7860
15
+
16
+ # Run the Flask app
17
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request
2
+ import os
3
+ from flask_sqlalchemy import SQLAlchemy
4
+
5
+
6
+ app = Flask(__name__)
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+ base_dir = os.path.abspath(os.path.dirname(__file__))
18
+ app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(base_dir, "data.db")}'
19
+ db = SQLAlchemy(app)
20
+
21
+
22
+
23
+ class Drink(db.Model):
24
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
25
+ name = db.Column(db.String(80), unique=True, nullable=False)
26
+ description = db.Column(db.String(120))
27
+
28
+
29
+ def __repr__(self):
30
+ return f"{self.name} - {self.description}"
31
+
32
+
33
+ @app.route('/')
34
+ def index():
35
+ return 'hello welcome to sanjeev page please use /drinks for all drinks /drinks/<id> for specific /drinks[POST] with name and description to update the list'
36
+
37
+
38
+ @app.route('/drinks')
39
+ def getdrinks():
40
+ drinks = Drink.query.all()
41
+ print(drinks)
42
+ output = []
43
+ for drink in drinks:
44
+ drink_data = {'name':drink.name, 'description':drink.description}
45
+
46
+ output.append(drink_data)
47
+
48
+ return {'drinks': output }
49
+
50
+ @app.route('/drinks/<id>')
51
+ def getdrink(id):
52
+ drink = Drink.query.get_or_404(id)
53
+ return {"name": drink.name , "description": drink.description}
54
+
55
+
56
+ @app.route('/drinks', methods=['POST'])
57
+ def add_drink():
58
+ drink = Drink(name=request.json['name'], description=request.json['description'])
59
+ db.session.add(drink)
60
+ db.session.commit()
61
+ return { 'id':drink.id}
62
+
63
+
64
+ @app.route('/drinks/<id>', methods=['DELETE'])
65
+ def deletedrink(id):
66
+ drink = Drink.query.get(id)
67
+ if drink is None:
68
+ return {"error":"not found"}
69
+ db.session.delete(drink)
70
+ db.session.commit()
71
+ return {"message":"deleted lol"}
72
+
73
+
74
+ # Ensure the table is created when the app is run
75
+ with app.app_context():
76
+ db.create_all()
77
+
78
+ # Required to run the app on Hugging Face Spaces
79
+ if __name__ == '__main__':
80
+ app.run(host='0.0.0.0', port=7860)
data.db ADDED
Binary file (12.3 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ blinker==1.9.0
2
+ click==8.1.7
3
+ Flask==3.1.0
4
+ Flask-SQLAlchemy==3.1.1
5
+ greenlet==3.1.1
6
+ itsdangerous==2.2.0
7
+ Jinja2==3.1.4
8
+ MarkupSafe==3.0.2
9
+ SQLAlchemy==2.0.36
10
+ typing_extensions==4.12.2
11
+ Werkzeug==3.1.3