Spaces:
Running
Running
File size: 3,494 Bytes
644ca8a 4d95901 57ebff8 bf5ebfe 644ca8a 4d95901 644ca8a 4d95901 644ca8a 84cb10d 0b89e32 84cb10d bf5ebfe 57ebff8 bf5ebfe 57ebff8 bf5ebfe 57ebff8 bf5ebfe 57ebff8 bf5ebfe |
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 |
import os
import streamlit as st
from azure.cosmos import CosmosClient, PartitionKey
from azure.storage.blob import BlobServiceClient
import requests
# Environment Variables
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
# Initialize Azure Cosmos DB Client
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
# Initialize Azure Blob Storage Client
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
# Function to Get Containers and Items
def get_containers_and_items(database_name):
database_client = cosmos_client.get_database_client(database_name)
containers_info = []
for container_properties in database_client.list_containers():
container_name = container_properties['id']
container_client = database_client.get_container_client(container_name)
items = list(container_client.read_all_items())
containers_info.append((container_name, items))
return containers_info
# UI to List Containers and Items in Predefined Databases
st.subheader('List Containers and Items in Databases')
databases_to_list = ["ToDoList", "CopilotSampleDb"]
for db_name in databases_to_list:
st.markdown(f"### Database: {db_name}")
containers_info = get_containers_and_items(db_name)
for container_name, items in containers_info:
st.subheader(f'Container: {container_name}')
for item in items:
st.json(item)
selected_db_for_listing = st.text_input('Enter Database Name to List Containers and Items')
emoji_button = st.button('🔍 Retrieve Data')
if emoji_button:
if selected_db_for_listing:
containers_info = get_containers_and_items(selected_db_for_listing)
for container_name, items in containers_info:
st.subheader(f'Container: {container_name}')
for item in items:
st.json(item)
else:
st.error('Please enter a database name.')
# Streamlit UI
st.title('Azure Services Integration with Streamlit')
# Azure Cosmos DB - CRUD Operations
st.subheader('Azure Cosmos DB - CRUD Operations')
cosmos_db = st.text_input('Database Name')
cosmos_container = st.text_input('Container Name')
item_id = st.text_input("Item ID (for Read, Update, Delete)")
item_data = st.text_area("Item Data (JSON format, for Create and Update)")
if st.button('Create Item in Cosmos DB'):
container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
container.create_item(item_data)
if st.button('Read Item from Cosmos DB'):
container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
item = container.read_item(item_id, partition_key=PartitionKey(item_id))
st.json(item)
# Azure Blob Storage - Upload/Download
st.subheader('Azure Blob Storage - Upload/Download')
blob_container = st.text_input('Blob Container')
blob_file = st.file_uploader('Upload file to Blob')
if blob_file is not None and st.button('Upload to Blob'):
blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
blob_client.upload_blob(blob_file.getvalue())
# Azure Functions - Trigger
st.subheader('Azure Functions - Trigger')
function_url = st.text_input('Function URL')
if st.button('Call Azure Function'):
response = requests.get(function_url)
st.write('Function Response:', response.text)
|