Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from azure.cosmos import CosmosClient
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Cosmos DB configuration
|
6 |
+
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
7 |
+
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
8 |
+
|
9 |
+
# You'll need to set these environment variables or use Azure Key Vault
|
10 |
+
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
11 |
+
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
12 |
+
|
13 |
+
def insert_record(record):
|
14 |
+
try:
|
15 |
+
response = container.create_item(body=record)
|
16 |
+
return True, response
|
17 |
+
except Exception as e:
|
18 |
+
return False, str(e)
|
19 |
+
|
20 |
+
# Streamlit app
|
21 |
+
st.title("๐ Cosmos DB Record Insertion")
|
22 |
+
|
23 |
+
# Login section
|
24 |
+
if 'logged_in' not in st.session_state:
|
25 |
+
st.session_state.logged_in = False
|
26 |
+
|
27 |
+
if not st.session_state.logged_in:
|
28 |
+
st.subheader("๐ Login")
|
29 |
+
input_key = st.text_input("Enter your Cosmos DB Primary Key", type="password")
|
30 |
+
if st.button("๐ Login"):
|
31 |
+
if input_key:
|
32 |
+
st.session_state.primary_key = input_key
|
33 |
+
st.session_state.logged_in = True
|
34 |
+
st.experimental_rerun()
|
35 |
+
else:
|
36 |
+
st.error("Please enter a valid key")
|
37 |
+
else:
|
38 |
+
# Initialize Cosmos DB client
|
39 |
+
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
40 |
+
database = client.get_database_client(DATABASE_NAME)
|
41 |
+
container = database.get_container_client(CONTAINER_NAME)
|
42 |
+
|
43 |
+
# Input fields
|
44 |
+
st.subheader("๐ Enter Record Details")
|
45 |
+
id = st.text_input("ID")
|
46 |
+
name = st.text_input("Name")
|
47 |
+
age = st.number_input("Age", min_value=0, max_value=150)
|
48 |
+
city = st.text_input("City")
|
49 |
+
|
50 |
+
# Submit button
|
51 |
+
if st.button("๐พ Insert Record"):
|
52 |
+
record = {
|
53 |
+
"id": id,
|
54 |
+
"name": name,
|
55 |
+
"age": age,
|
56 |
+
"city": city
|
57 |
+
}
|
58 |
+
|
59 |
+
success, response = insert_record(record)
|
60 |
+
if success:
|
61 |
+
st.success("โ
Record inserted successfully!")
|
62 |
+
st.json(response)
|
63 |
+
else:
|
64 |
+
st.error(f"โ Failed to insert record: {response}")
|
65 |
+
|
66 |
+
# Logout button
|
67 |
+
if st.button("๐ช Logout"):
|
68 |
+
st.session_state.logged_in = False
|
69 |
+
st.experimental_rerun()
|
70 |
+
|
71 |
+
# Display connection info
|
72 |
+
st.sidebar.subheader("๐ Connection Information")
|
73 |
+
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
74 |
+
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
75 |
+
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
76 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|