Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,86 +7,97 @@ app = Flask(__name__, template_folder="./")
|
|
| 7 |
|
| 8 |
# Создание базы данных и таблицы
|
| 9 |
def init_db():
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Маршрут для обработки GET-запроса
|
| 23 |
@app.route('/add_contact', methods=['GET'])
|
| 24 |
def add_contact():
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Маршрут для отображения таблицы контактов
|
| 40 |
@app.route('/contacts')
|
| 41 |
def show_contacts():
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if __name__ == '__main__':
|
| 92 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 7 |
|
| 8 |
# Создание базы данных и таблицы
|
| 9 |
def init_db():
|
| 10 |
+
try:
|
| 11 |
+
conn = sqlite3.connect('data.db')
|
| 12 |
+
cursor = conn.cursor()
|
| 13 |
+
cursor.execute('''
|
| 14 |
+
CREATE TABLE IF NOT EXISTS contacts (
|
| 15 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 16 |
+
name TEXT NOT NULL,
|
| 17 |
+
phone TEXT NOT NULL
|
| 18 |
+
)
|
| 19 |
+
''')
|
| 20 |
+
conn.commit()
|
| 21 |
+
conn.close()
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Error initializing database: {e}")
|
| 24 |
|
| 25 |
# Маршрут для обработки GET-запроса
|
| 26 |
@app.route('/add_contact', methods=['GET'])
|
| 27 |
def add_contact():
|
| 28 |
+
try:
|
| 29 |
+
name = request.args.get('name')
|
| 30 |
+
phone = request.args.get('phone')
|
| 31 |
|
| 32 |
+
if not name or not phone:
|
| 33 |
+
return "Both 'name' and 'phone' parameters are required.", 400
|
| 34 |
|
| 35 |
+
conn = sqlite3.connect('data.db')
|
| 36 |
+
cursor = conn.cursor()
|
| 37 |
+
cursor.execute('INSERT INTO contacts (name, phone) VALUES (?, ?)', (name, phone))
|
| 38 |
+
conn.commit()
|
| 39 |
+
conn.close()
|
| 40 |
|
| 41 |
+
return f"Contact added: {name} - {phone}", 200
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error adding contact: {e}")
|
| 44 |
+
return "Internal Server Error", 500
|
| 45 |
|
| 46 |
# Маршрут для отображения таблицы контактов
|
| 47 |
@app.route('/contacts')
|
| 48 |
def show_contacts():
|
| 49 |
+
try:
|
| 50 |
+
conn = sqlite3.connect('data.db')
|
| 51 |
+
cursor = conn.cursor()
|
| 52 |
+
cursor.execute('SELECT name, phone FROM contacts')
|
| 53 |
+
contacts = cursor.fetchall()
|
| 54 |
+
conn.close()
|
| 55 |
|
| 56 |
+
# HTML-шаблон для отображения таблицы
|
| 57 |
+
html = '''
|
| 58 |
+
<!doctype html>
|
| 59 |
+
<html lang="en">
|
| 60 |
+
<head>
|
| 61 |
+
<meta charset="utf-8">
|
| 62 |
+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
| 63 |
+
<title>Contacts</title>
|
| 64 |
+
<style>
|
| 65 |
+
table {
|
| 66 |
+
width: 50%;
|
| 67 |
+
border-collapse: collapse;
|
| 68 |
+
}
|
| 69 |
+
th, td {
|
| 70 |
+
border: 1px solid black;
|
| 71 |
+
padding: 8px;
|
| 72 |
+
text-align: left;
|
| 73 |
+
}
|
| 74 |
+
th {
|
| 75 |
+
background-color: #f2f2f2;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
</head>
|
| 79 |
+
<body>
|
| 80 |
+
<h1>Contacts</h1>
|
| 81 |
+
<table>
|
| 82 |
+
<tr>
|
| 83 |
+
<th>Name</th>
|
| 84 |
+
<th>Phone</th>
|
| 85 |
+
</tr>
|
| 86 |
+
{% for contact in contacts %}
|
| 87 |
+
<tr>
|
| 88 |
+
<td>{{ contact[0] }}</td>
|
| 89 |
+
<td>{{ contact[1] }}</td>
|
| 90 |
+
</tr>
|
| 91 |
+
{% endfor %}
|
| 92 |
+
</table>
|
| 93 |
+
</body>
|
| 94 |
+
</html>
|
| 95 |
+
'''
|
| 96 |
|
| 97 |
+
return render_template_string(html, contacts=contacts)
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"Error showing contacts: {e}")
|
| 100 |
+
return "Internal Server Error", 500
|
| 101 |
|
| 102 |
if __name__ == '__main__':
|
| 103 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|