Skip to content

Commit 33b4e7d

Browse files
authored
Add files via upload
1 parent 5fc53df commit 33b4e7d

File tree

12 files changed

+335
-0
lines changed

12 files changed

+335
-0
lines changed

13-Flask/flask/api.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### Put and Delete-HTTP Verbs
2+
### Working With API's--Json
3+
4+
from flask import Flask, jsonify, request
5+
6+
app = Flask(__name__)
7+
8+
##Initial Data in my to do list
9+
items = [
10+
{"id": 1, "name": "Item 1", "description": "This is item 1"},
11+
{"id": 2, "name": "Item 2", "description": "This is item 2"}
12+
]
13+
14+
@app.route('/')
15+
def home():
16+
return "Welcome To The Sample To DO List App"
17+
18+
## Get: Retrieve all the items
19+
20+
@app.route('/items',methods=['GET'])
21+
def get_items():
22+
return jsonify(items)
23+
24+
## get: Retireve a specific item by Id
25+
@app.route('/items/<int:item_id>',methods=['GET'])
26+
def get_item(item_id):
27+
item=next((item for item in items if item["id"]==item_id),None)
28+
if item is None:
29+
return jsonify({"error":"item not found"})
30+
return jsonify(item)
31+
32+
## Post :create a new task- API
33+
@app.route('/items',methods=['POST'])
34+
def create_item():
35+
if not request.json or not 'name' in request.json:
36+
return jsonify({"error":"item not found"})
37+
new_item={
38+
"id": items[-1]["id"] + 1 if items else 1,
39+
"name":request.json['name'],
40+
"description":request.json["description"]
41+
42+
43+
}
44+
items.append(new_item)
45+
return jsonify(new_item)
46+
47+
# Put: Update an existing item
48+
@app.route('/items/<int:item_id>',methods=['PUT'])
49+
def update_item(item_id):
50+
item = next((item for item in items if item["id"] == item_id), None)
51+
if item is None:
52+
return jsonify({"error": "Item not found"})
53+
item['name'] = request.json.get('name', item['name'])
54+
item['description'] = request.json.get('description', item['description'])
55+
return jsonify(item)
56+
57+
# DELETE: Delete an item
58+
@app.route('/items/<int:item_id>', methods=['DELETE'])
59+
def delete_item(item_id):
60+
global items
61+
items = [item for item in items if item["id"] != item_id]
62+
return jsonify({"result": "Item deleted"})
63+
64+
65+
66+
67+
if __name__ == '__main__':
68+
app.run(debug=True)

13-Flask/flask/app.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask import Flask
2+
'''
3+
It creates an instance of the Flask class,
4+
which will be your WSGI (Web Server Gateway Interface) application.
5+
'''
6+
###WSGI Application
7+
app=Flask(__name__)
8+
9+
@app.route("/")
10+
def welcome():
11+
return "Welcome to this best Flask course.This should be an amazing course"
12+
13+
@app.route("/index")
14+
def index():
15+
return "Welcome to the index page"
16+
17+
18+
if __name__=="__main__":
19+
app.run(debug=True)

13-Flask/flask/getpost.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from flask import Flask,render_template,request
2+
'''
3+
It creates an instance of the Flask class,
4+
which will be your WSGI (Web Server Gateway Interface) application.
5+
'''
6+
###WSGI Application
7+
app=Flask(__name__)
8+
9+
@app.route("/")
10+
def welcome():
11+
return "<html><H1>Welcome to the flask course</H1></html>"
12+
13+
@app.route("/index",methods=['GET'])
14+
def index():
15+
return render_template('index.html')
16+
17+
@app.route('/about')
18+
def about():
19+
return render_template('about.html')
20+
21+
@app.route('/form',methods=['GET','POST'])
22+
def form():
23+
if request.method=='POST':
24+
name=request.form['name']
25+
return f'Hello {name}!'
26+
return render_template('form.html')
27+
28+
@app.route('/submit',methods=['GET','POST'])
29+
def submit():
30+
if request.method=='POST':
31+
name=request.form['name']
32+
return f'Hello {name}!'
33+
return render_template('form.html')
34+
35+
36+
if __name__=="__main__":
37+
app.run(debug=True)

13-Flask/flask/jinja.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### Building Url Dynamically
2+
## Variable Rule
3+
### Jinja 2 Template Engine
4+
5+
### Jinja2 Template Engine
6+
'''
7+
{{ }} expressions to print output in html
8+
{%...%} conditions, for loops
9+
{#...#} this is for comments
10+
'''
11+
12+
from flask import Flask,render_template,request,redirect,url_for
13+
'''
14+
It creates an instance of the Flask class,
15+
which will be your WSGI (Web Server Gateway Interface) application.
16+
'''
17+
###WSGI Application
18+
app=Flask(__name__)
19+
20+
@app.route("/")
21+
def welcome():
22+
return "<html><H1>Welcome to the flask course</H1></html>"
23+
24+
@app.route("/index",methods=['GET'])
25+
def index():
26+
return render_template('index.html')
27+
28+
@app.route('/about')
29+
def about():
30+
return render_template('about.html')
31+
32+
33+
34+
## Variable Rule
35+
@app.route('/success/<int:score>')
36+
def success(score):
37+
res=""
38+
if score>=50:
39+
res="PASSED"
40+
else:
41+
res="FAILED"
42+
43+
return render_template('result.html',results=res)
44+
45+
## Variable Rule
46+
@app.route('/successres/<int:score>')
47+
def successres(score):
48+
res=""
49+
if score>=50:
50+
res="PASSED"
51+
else:
52+
res="FAILED"
53+
54+
exp={'score':score,"res":res}
55+
56+
return render_template('result1.html',results=exp)
57+
58+
## if confition
59+
@app.route('/sucessif/<int:score>')
60+
def successif(score):
61+
62+
return render_template('result.html',results=score)
63+
64+
@app.route('/fail/<int:score>')
65+
def fail(score):
66+
return render_template('result.html',results=score)
67+
68+
@app.route('/submit',methods=['POST','GET'])
69+
def submit():
70+
total_score=0
71+
if request.method=='POST':
72+
science=float(request.form['science'])
73+
maths=float(request.form['maths'])
74+
c=float(request.form['c'])
75+
data_science=float(request.form['datascience'])
76+
77+
total_score=(science+maths+c+data_science)/4
78+
else:
79+
return render_template('getresult.html')
80+
return redirect(url_for('successres',score=total_score))
81+
82+
83+
84+
85+
86+
87+
if __name__=="__main__":
88+
app.run(debug=True)
89+

13-Flask/flask/main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from flask import Flask,render_template
2+
'''
3+
It creates an instance of the Flask class,
4+
which will be your WSGI (Web Server Gateway Interface) application.
5+
'''
6+
###WSGI Application
7+
app=Flask(__name__)
8+
9+
@app.route("/")
10+
def welcome():
11+
return "<html><H1>Welcome to the flask course</H1></html>"
12+
13+
@app.route("/index")
14+
def index():
15+
return render_template('index.html')
16+
17+
@app.route('/about')
18+
def about():
19+
return render_template('about.html')
20+
21+
22+
if __name__=="__main__":
23+
app.run(debug=True)

13-Flask/flask/sample.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"name": "New Item", "description": "This is a new item"}
2+
3+
{"name": "Updated Item", "description": "This item has been updated"}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>About</title>
6+
</head>
7+
<body>
8+
<h1>About</h1>
9+
<p>This is the about page of my Flask app.</p>
10+
</body>
11+
</html>

13-Flask/flask/templates/form.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Form</title>
6+
</head>
7+
<body>
8+
<h1>Submit a Form</h1>
9+
<form action="/submit" method="post">
10+
<label for="name">Name:</label>
11+
<input type="text" id="name" name="name">
12+
<input type="submit" value="Submit">
13+
</form>
14+
</body>
15+
</html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="{{ url_for('static',filename='css/style.css') }}">
5+
<script type="text/javascript" src="{{ url_for('static',filename='script/script.js') }}">
6+
7+
</script>
8+
9+
</head>
10+
<body>
11+
12+
<h2>HTML Forms</h2>
13+
14+
<form action="/submit" method='post'>
15+
<label for="Science">Science:</label><br>
16+
<input type="text" id="science" name="science" value="0"><br>
17+
<label for="Maths">Maths:</label><br>
18+
<input type="text" id="maths" name="maths" value="0"><br><br>
19+
<label for="C ">C:</label><br>
20+
<input type="text" id="c" name="c" value="0"><br><br>
21+
<label for="datascience">Data Science:</label><br>
22+
<input type="text" id="datascience" name="datascience" value="0"><br><br>
23+
<input type="submit" value="Submit">
24+
</form>
25+
26+
<p>If you click the "Submit" button, the form-data will be sent to a page called "/submit".</p>
27+
28+
</body>
29+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Flask App</title>
6+
</head>
7+
<body>
8+
<h1>Welcome to My Flask App!</h1>
9+
<p>This is a simple web application built with Flask.</p>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)