-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_db_app.py
113 lines (87 loc) · 2.92 KB
/
cloud_db_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# A mini app to connect to and store data on a cloud based instance of PostgreSQL
# Works by being associated with a Heroku app that has the database
from tkinter import *
import psycopg2
import os
from dotenv import load_dotenv
import subprocess
root = Tk()
root.title('Postgres Cloud App')
root.geometry("500x550")
def get_env_var():
# remove current database url variable from the dictionary of environment variables
try:
del os.environ['DATABASE_URL']
except KeyError:
pass
# Run bash script with shell
subprocess.call("get_env.sh", shell=True)
# Load environment variable into os.environ dictionary
load_dotenv()
'''
for i, j in os.environ.items():
print(i, j)
'''
def query():
# Assign database url environment variable to database_url
DATABASE_URL = os.environ['DATABASE_URL']
# Configure and connect to Postgres
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# Create a cursor
c = conn.cursor()
# Create a table
c.execute('''CREATE TABLE IF NOT EXISTS customers
(first_name TEXT,
last_name TEXT);
''')
conn.commit()
conn.close()
def submit():
first_name = f_name.get()
last_name = l_name.get()
DATABASE_URL = os.environ['DATABASE_URL']
# Configure and connect to Postgres
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# Create a cursor
c = conn.cursor()
# Insert data into table
c.execute("INSERT INTO customers VALUES (%s, %s)", (first_name, last_name))
conn.commit()
conn.close()
# Update app with new records from database
update()
def update():
DATABASE_URL = os.environ['DATABASE_URL']
# Configure and connect to Postgres
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# Create a cursor
c = conn.cursor()
# Grab stuff from database
c.execute("SELECT * FROM customers")
records = c.fetchall()
output = ""
for record in records:
output_label.config(text=f"{output}\n{record[0]} {record[1]}")
output = output_label['text']
conn.commit()
conn.close()
# Create the GUI for the app
my_frame = LabelFrame(root, text="Postgres Example")
my_frame.pack(pady=20)
f_label = Label(my_frame, text="First Name:")
f_label.grid(row=0, column=0, pady=10)
f_name = Entry(my_frame, font=("Helvetica, 18"))
f_name.grid(row=0, column=1, pady=10, padx=10)
l_label = Label(my_frame, text="Last Name:")
l_label.grid(row=1, column=0, pady=10, padx=10)
l_name = Entry(my_frame, font=("Helvetica, 18"))
l_name.grid(row=1, column=1, pady=10, padx=10)
submit_button = Button(my_frame, text="Submit", command=submit)
submit_button.grid(row=2, column=0, pady=10, padx=10)
update_button = Button(my_frame, text="Update", command=update)
update_button.grid(row=2, column=1, pady=10, padx=10)
output_label = Label(root, text="")
output_label.pack(pady=50)
get_env_var()
query()
root.mainloop()