DmitrMakeev commited on
Commit
e0c01fb
1 Parent(s): edee679

Update app.py

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