import streamlit as st
from src.utils.db import DataBaseManager
import pandas as pd
def main():
st.title("Profiles 👥")
st.write("Manage your contact profiles here.")
code
db = DataBaseManager()
# Form to add a new contact
st.subheader("Add a New Contact")
with st.form("contact_form", clear_on_submit=True):
col = st.columns(2)
with col[0]:
name = st.text_input("Name")
email = st.text_input("Email")
with col[1]:
title = st.text_input("Title")
profession = st.text_input("Profession")
notes = st.text_area("Notes")
submitted = st.form_submit_button("Add Contact")
if submitted:
contact_data = {
'name': name,
'email': email,
'title': title,
'profession': profession,
'notes': notes
}
db.add_contact_profile(contact_data)
st.success(f"Contact '{name}' added successfully!")
# st.rerun()
# Display existing profiles
st.subheader("Existing Contacts")
contacts = db.list_contact_profiles()
if contacts:
df = pd.DataFrame(contacts)
st.dataframe(df)
else:
st.info("No contacts found.")
if name == "main":
main()
Assistant
To add a "Delete Contact" button to your Streamlit app for each contact, you need to modify the existing code to include a delete functionality. Here's how you can do it:
Modify the Display Section: Instead of displaying the contacts in a DataFrame (which doesn't support interactive elements like buttons), loop over each contact and display the details with a "Delete" button.
Implement the Delete Functionality: When the "Delete" button is clicked, call a method from your DataBaseManager to remove the contact from the database, and then refresh the page to reflect the changes.
Here's the updated code with comments explaining each step:
python
import streamlit as st
from src.utils.db import DataBaseManager
import pandas as pd
def main():
st.title("Profiles 👥")
st.write("Manage your contact profiles here.")
db = DataBaseManager()
# Form to add a new contact
st.subheader("Add a New Contact")
with st.form("contact_form", clear_on_submit=True):
col = st.columns(2)
with col[0]:
name = st.text_input("Name")
email = st.text_input("Email")
with col[1]:
title = st.text_input("Title")
profession = st.text_input("Profession")
notes = st.text_area("Notes")
submitted = st.form_submit_button("Add Contact")
if submitted:
contact_data = {
'name': name,
'email': email,
'title': title,
'profession': profession,
'notes': notes
}
db.add_contact_profile(contact_data)
st.success(f"Contact '{name}' added successfully!")
# st.rerun()
# Display existing profiles with delete functionality
st.subheader("Existing Contacts")
contacts = db.list_contact_profiles()
if contacts:
for contact in contacts:
# Use an expander for each contact to keep the interface clean
with st.expander(f"{contact['name']} ({contact['email']})"):
st.write(f"**Title:** {contact['title']}")
st.write(f"**Profession:** {contact['profession']}")
st.write(f"**Notes:** {contact['notes']}")
# Add a Delete button for each contact
if st.button("Delete Contact", key=f"delete_{contact['id']}"):
# Call the delete method from your database manager
db.delete_contact_profile(contact['id'])
st.success(f"Contact '{contact['name']}' deleted successfully!")
st.experimental_rerun() # Refresh the page to update the list
else:
st.info("No contacts found.")
if __name__ == "__main__":
main()
Explanation:
Using Expanders: The st.expander widget allows you to display each contact's details in a collapsible section, keeping the interface organized.
Unique Keys for Buttons: When creating buttons in a loop, assign a unique key to each button to prevent Streamlit from confusing them. Using the contact's ID ensures uniqueness.
Delete Method in DataBaseManager: Ensure your DataBaseManager class has a delete_contact_profile method that accepts a contact's unique identifier (like id) and removes it from the database.
Modify DataBaseManager Class:
python
class DataBaseManager:
# Other methods...
def delete_contact_profile(self, contact_id):
# Implement the logic to delete the contact from the database
# For example:
# self.conn.execute("DELETE FROM contacts WHERE id = ?", (contact_id,))
# self.conn.commit()
pass # Replace with actual code
Notes:
Refreshing the Page: After deleting a contact, st.experimental_rerun() is called to refresh the app and display the updated list of contacts.
Handling Unique Identifiers: Make sure each contact has a unique identifier (id). If your database does not automatically assign IDs, consider modifying your add_contact_profile method to include this feature.
By implementing these changes, your Streamlit app will now display a "Delete Contact" button for each contact, allowing users to remove contacts as needed.