Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true,
"justMyCode": true
}
]
}
89 changes: 89 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from flask import Flask
from datetime import datetime
from flask import render_template
from flask import request

app = Flask(__name__)

@app.route("/")
def index():
now = datetime.now()
user = {'username': 'Анатолий'}
if now.hour > 5 and now.hour <= 12:
time_of_day = "morning"
elif now.hour > 12 and now.hour <= 17:
time_of_day = "day"
elif now.hour > 17 and now.hour <= 23:
time_of_day = "evening"
else:
time_of_day = "night"

items = ["один","два", "три", "четыре", "пять" ]

return render_template('index.html', title='Home', user = user, time_of_day = time_of_day, items = items)

@app.route("/hello")
def hello_world():
now = datetime.now()
return f"<p>Hello, World! {now}</p>"


@app.route("/simp_calc")
def simple():
a = request.args.get("a", default = 0, type = float)
b = request.args.get("b", default = 0, type = float)
return f"<h1> {a} + {b} = { a + b }</h1>"


@app.route("/styled")
def hello_styled():
now = datetime.now()
return f"""
<h1> Заголовок </h1>
<p> Hello, world!</p>
<p> Текущее время : {now} </p>
"""

@app.route('/calc', methods=['GET', 'POST'])
def calc():
a = request.form.get("a", default = 0, type = float)
b = request.form.get("b", default = 0, type = float)
print(a,b)
return render_template("calc.html", a = a, b = b, result = a + b)

import cat

cats = {

"vasya": cat.Cat("vasya", 3),
"barsik": cat.Cat("barsik", 4),
"murzik": cat.Cat("murzik", 1),
}

@app.route('/cats')
def list_cats():
return render_template("cats.html", cats = cats)

@app.route('/cats/<name>')
def fish(name):
cat = cats[name]
return render_template("cat.html", cat = cat)

@app.route("/cats/new", methods=["GET", "POST"])
def create_cat():
name = request.form.get("name",default="untitled")
age = request.form.get("age",default=0,type=float)

new_cat = cat.Cat(name, age)

cats[new_cat.name] = new_cat

return render_template("new_cat.html")

from flask import jsonify
@app.route("/api/cats")
def api_cats():
result = []
for key, item in cats:
result.append(item.toJson())
return jsonify(result)
5 changes: 5 additions & 0 deletions cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Cat:
def __init__(self, name, age, img = ""):
self.name = name
self.age = age
self.img = img
8 changes: 8 additions & 0 deletions templates/base_cat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Cat</title>
</head>
<body>
{% block main %}{% endblock %}
</body>
</html>
15 changes: 15 additions & 0 deletions templates/calc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<form action="" method="POST">
<input type="number" value="{{a}}" name="a"/>
+
<input type="number" value="{{b}}" name="b"/>
<button type="submit">Calc</button>
</form>
<p>Result = {{result}}</p>
</body>
</html>
14 changes: 14 additions & 0 deletions templates/cat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base_cat.html" %}
{% block main %}
<h1>{{cat.name}}:</h1>
<ul>
<li>
Возраст: {{ cat.age }}
</li>
{% if cat.img %}
<li>
<img src="{{cat.img}}"/>
</li>
{% endif %}
</ul>
{% endblock %}
13 changes: 13 additions & 0 deletions templates/cats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base_cat.html" %}
{% block main %}
<h1>Котики:</h1>
<ul>
{% for key, cat in cats.items() %}
<li>
<a href="{{'cats/%s' % cat.name}}"> {{cat.name }}</a>
</li>
{% endfor %}
</ul>
<a href="cats/new" class="btn btn-primary" role="button">Добавить</a>

{% endblock %}
21 changes: 21 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<title>{{ title }} - Microblog</title>
</head>
<body>
{% if time_of_day == "morning" %}
<h1>Доброе утро, {{ user.username }}!</h1>
{% elif time_of_day == "day" %}
<h1>добрый день, {{ user.username }}!</h1>
{% elif time_of_day == "evening" %}
<h1>Добрый вечер, {{ user.username }}!</h1>
{% elif time_of_day == "night" %}
<h1>Доброй ночи, {{ user.username }}!</h1>
{% endif %}
<ul>
{% for element in items %}
<li>{{element}}</li>>
{% endfor %}
</ul>
</body>
</html>
14 changes: 14 additions & 0 deletions templates/new_cat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base_cat.html" %}
{% block main %}
<h1>Котик:</h1>
<form action="" method="POST">
<p>Имя:
<input name="name"/>
</p>
<p>Возраст:
<input name="age" type="number"/>
</p>
<button type="submit">Сохранить</button>
</form>

{% endblock %}
22 changes: 22 additions & 0 deletions work.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,27 @@
"eamodio.gitlens",
"mhutchie.git-graph"
]
},
"launch": {
"version": "0.2.0",
"configurations": [

{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true,
"justMyCode": true
}
]
}
}
42 changes: 42 additions & 0 deletions zachet.work.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"folders": [
{
"name": "Configs(root)",
"path": "."
}
],
"settings": {
"files.exclude": {
"**/.git": true,
},
},
"extensions": {
"recommendations": [
"ms-python.python",
"eamodio.gitlens",
"mhutchie.git-graph"
]
},
"launch": {
"version": "0.2.0",
"configurations": [

{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--no-debugger"
],
"jinja": true,
"justMyCode": true
}
]
}
}