Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import json
|
| 3 |
from azure.cosmos import CosmosClient
|
| 4 |
-
|
| 5 |
-
# Initialize variables
|
| 6 |
-
connection_str = ""
|
| 7 |
-
database_name = ""
|
| 8 |
-
container_name = ""
|
| 9 |
|
| 10 |
# Load and save query files
|
| 11 |
def load_query(filename):
|
|
@@ -16,31 +11,32 @@ def save_query(filename, query):
|
|
| 16 |
with open(filename, 'w') as file:
|
| 17 |
file.write(query)
|
| 18 |
|
| 19 |
-
# Connect to Azure Cosmos DB
|
| 20 |
-
def connect_to_cosmosdb(url, key, database, container):
|
| 21 |
-
client = CosmosClient(url, credential=key)
|
| 22 |
-
db = client.get_database_client(database)
|
| 23 |
-
container = db.get_container_client(container)
|
| 24 |
-
return container
|
| 25 |
-
|
| 26 |
# Streamlit UI
|
| 27 |
st.title("Azure Cosmos DB Explorer π½")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Connection Details Expander
|
| 30 |
with st.expander("Connect π"):
|
| 31 |
-
|
| 32 |
-
database_name = st.text_input("Database Name:",
|
| 33 |
-
container_name = st.text_input("Container Name:",
|
| 34 |
if st.button("Connect"):
|
| 35 |
try:
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
st.success("Connected successfully! π")
|
| 38 |
except Exception as e:
|
| 39 |
st.error(f"Failed to connect: {e}")
|
| 40 |
|
| 41 |
# Query Editor Expander
|
| 42 |
with st.expander("Query Editor π"):
|
| 43 |
-
query = st.text_area("Enter your SQL query here:")
|
| 44 |
file_option = st.selectbox("File Options", ["New", "Open", "Save", "Save As"])
|
| 45 |
|
| 46 |
if file_option == "New":
|
|
@@ -59,9 +55,22 @@ with st.expander("Query Editor π"):
|
|
| 59 |
save_query(saveas_filename, query)
|
| 60 |
|
| 61 |
if st.button("Execute Query π"):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from azure.cosmos import CosmosClient
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load and save query files
|
| 6 |
def load_query(filename):
|
|
|
|
| 11 |
with open(filename, 'w') as file:
|
| 12 |
file.write(query)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Streamlit UI
|
| 15 |
st.title("Azure Cosmos DB Explorer π½")
|
| 16 |
|
| 17 |
+
# Environment Variables for Cosmos DB Credentials
|
| 18 |
+
ACCOUNT_URI = os.environ.get('ACCOUNT_URI', 'Your Cosmos DB URI here')
|
| 19 |
+
ACCOUNT_KEY = os.environ.get('ACCOUNT_KEY', 'Your Cosmos DB Key here')
|
| 20 |
+
|
| 21 |
+
client = None
|
| 22 |
+
|
| 23 |
# Connection Details Expander
|
| 24 |
with st.expander("Connect π"):
|
| 25 |
+
st.write("Environment Variables for Cosmos DB Credentials are pre-loaded.")
|
| 26 |
+
database_name = st.text_input("Database Name:", "")
|
| 27 |
+
container_name = st.text_input("Container Name:", "")
|
| 28 |
if st.button("Connect"):
|
| 29 |
try:
|
| 30 |
+
client = CosmosClient(ACCOUNT_URI, credential=ACCOUNT_KEY)
|
| 31 |
+
database_client = client.get_database_client(database_name)
|
| 32 |
+
container_client = database_client.get_container_client(container_name)
|
| 33 |
st.success("Connected successfully! π")
|
| 34 |
except Exception as e:
|
| 35 |
st.error(f"Failed to connect: {e}")
|
| 36 |
|
| 37 |
# Query Editor Expander
|
| 38 |
with st.expander("Query Editor π"):
|
| 39 |
+
query = st.text_area("Enter your SQL query here:", "")
|
| 40 |
file_option = st.selectbox("File Options", ["New", "Open", "Save", "Save As"])
|
| 41 |
|
| 42 |
if file_option == "New":
|
|
|
|
| 55 |
save_query(saveas_filename, query)
|
| 56 |
|
| 57 |
if st.button("Execute Query π"):
|
| 58 |
+
if client:
|
| 59 |
+
try:
|
| 60 |
+
items = list(container_client.query_items(
|
| 61 |
+
query=query,
|
| 62 |
+
enable_cross_partition_query=True
|
| 63 |
+
))
|
| 64 |
+
st.write("Results π:")
|
| 65 |
+
st.json(items)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
st.error(f"Query failed: {e}")
|
| 68 |
+
else:
|
| 69 |
+
st.warning("Not connected to any Cosmos DB. Please connect first.")
|
| 70 |
+
|
| 71 |
+
# Instructions
|
| 72 |
+
st.markdown("""
|
| 73 |
+
## Instructions to Run this App:
|
| 74 |
+
|
| 75 |
+
1. **Environment Variables**: Set the `ACCOUNT_URI` and `ACCOUNT_KEY` environment variables with your Azure Cosmos DB URI and Key respectively.
|
| 76 |
+
2. **Install Packages**: If you haven't, install the required Python packages:
|