Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,49 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import psycopg2
|
| 3 |
import os
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def connect_db():
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
def insert_user(company_name, requirements, date, status, notes, next_steps, contact_person, contact_information):
|
| 16 |
conn = connect_db()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
query = """
|
| 20 |
-
INSERT INTO users
|
| 21 |
-
(company_name, requirements, date, status, notes, next_steps, contact_person, contact_information)
|
| 22 |
-
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
| 23 |
-
"""
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
st.title("Add a New User")
|
| 33 |
|
| 34 |
with st.form("user_form"):
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import psycopg2
|
| 3 |
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Connect to PostgreSQL database
|
| 11 |
def connect_db():
|
| 12 |
+
try:
|
| 13 |
+
return psycopg2.connect(
|
| 14 |
+
host=os.getenv("DB_HOST"),
|
| 15 |
+
port=os.getenv("DB_PORT", 5432),
|
| 16 |
+
database=os.getenv("DB_NAME"),
|
| 17 |
+
user=os.getenv("DB_USER"),
|
| 18 |
+
password=os.getenv("DB_PASSWORD")
|
| 19 |
+
)
|
| 20 |
+
except psycopg2.OperationalError as e:
|
| 21 |
+
st.error(f"Error connecting to the database: {e}")
|
| 22 |
+
return None
|
| 23 |
|
| 24 |
+
# Insert user data into the "users" table
|
| 25 |
def insert_user(company_name, requirements, date, status, notes, next_steps, contact_person, contact_information):
|
| 26 |
conn = connect_db()
|
| 27 |
+
if conn is None:
|
| 28 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
cur = conn.cursor()
|
| 32 |
+
query = """
|
| 33 |
+
INSERT INTO users
|
| 34 |
+
(company_name, requirements, date, status, notes, next_steps, contact_person, contact_information)
|
| 35 |
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
| 36 |
+
"""
|
| 37 |
+
cur.execute(query, (company_name, requirements, date, status, notes, next_steps, contact_person, contact_information))
|
| 38 |
+
conn.commit()
|
| 39 |
+
cur.close()
|
| 40 |
+
st.success("User added successfully!")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"Error inserting user data: {e}")
|
| 43 |
+
finally:
|
| 44 |
+
conn.close()
|
| 45 |
|
| 46 |
+
# Streamlit UI
|
| 47 |
st.title("Add a New User")
|
| 48 |
|
| 49 |
with st.form("user_form"):
|