Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,61 @@
|
|
1 |
import streamlit as st
|
2 |
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
|
|
|
3 |
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
blob_client.upload_blob(file, overwrite=True)
|
14 |
|
15 |
-
def list_blobs():
|
16 |
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
|
17 |
container_client = blob_service_client.get_container_client(container_name)
|
18 |
|
19 |
blob_list = container_client.list_blobs()
|
20 |
for blob in blob_list:
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def app():
|
24 |
-
st.title('Azure Blob Storage App')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
if st.button('List Files'):
|
31 |
-
list_blobs()
|
32 |
|
33 |
if __name__ == "__main__":
|
34 |
app()
|
|
|
1 |
import streamlit as st
|
2 |
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
|
3 |
+
import base64
|
4 |
import os
|
5 |
|
6 |
+
# Session State workaround
|
7 |
+
class SessionState(object):
|
8 |
+
def __init__(self, **kwargs):
|
9 |
+
for key, val in kwargs.items():
|
10 |
+
setattr(self, key, val)
|
11 |
|
12 |
+
|
13 |
+
def get_state(**kwargs):
|
14 |
+
if 'session_state' not in st.session_state:
|
15 |
+
st.session_state['session_state'] = SessionState(**kwargs)
|
16 |
+
return st.session_state['session_state']
|
17 |
+
|
18 |
+
|
19 |
+
def save_to_azure(files, connect_str, container_name):
|
20 |
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
|
21 |
+
for file in files:
|
22 |
+
blob_client = blob_service_client.get_blob_client(container_name, file.name)
|
23 |
+
blob_client.upload_blob(file, overwrite=True)
|
24 |
|
|
|
25 |
|
26 |
+
def list_blobs(connect_str, container_name):
|
27 |
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
|
28 |
container_client = blob_service_client.get_container_client(container_name)
|
29 |
|
30 |
blob_list = container_client.list_blobs()
|
31 |
for blob in blob_list:
|
32 |
+
blob_url = f"https://{blob_service_client.account_name}.blob.core.windows.net/{container_name}/{blob.name}"
|
33 |
+
b64 = base64.b64encode(blob_url.encode()).decode()
|
34 |
+
href = f'<a href="data:file/octet-stream;base64,{b64}" download="{blob.name}">Download {blob.name}</a>'
|
35 |
+
st.markdown(href, unsafe_allow_html=True)
|
36 |
+
|
37 |
|
38 |
def app():
|
39 |
+
st.title('Azure Blob Storage App 💾')
|
40 |
+
|
41 |
+
state = get_state(connect_str='', container_name='')
|
42 |
+
|
43 |
+
with st.sidebar:
|
44 |
+
st.subheader("Azure Settings 🔧")
|
45 |
+
state.connect_str = st.text_input('Connection String', value=state.connect_str)
|
46 |
+
state.container_name = st.text_input('Container Name', value=state.container_name)
|
47 |
+
|
48 |
+
st.subheader("Your documents 📑")
|
49 |
+
docs = st.file_uploader("Import documents", accept_multiple_files=True)
|
50 |
+
|
51 |
+
if st.button('Save Files'):
|
52 |
+
with st.spinner("Saving..."):
|
53 |
+
save_to_azure(docs, state.connect_str, state.container_name)
|
54 |
|
55 |
+
if st.button('Retrieve Files'):
|
56 |
+
with st.spinner("Retrieving..."):
|
57 |
+
list_blobs(state.connect_str, state.container_name)
|
58 |
|
|
|
|
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
app()
|