-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebfunctions.py
More file actions
73 lines (57 loc) · 2.27 KB
/
Copy pathwebfunctions.py
File metadata and controls
73 lines (57 loc) · 2.27 KB
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
import streamlit as st
from database import query_db
from datetime import datetime
from filefunctions import export_to_csv
from filefunctions import string_to_filename
import base64
def channel_name_dropdown():
# Fetch rows into lists
channel_names_from_database = []
channel_ids_from_database = []
query = "select Id, DisplayName from Channels"
for row in query_db(query):
channel_id, channel_name = row # row returned is tuple ("id", "name"), we split it here
if channel_name != "":
channel_ids_from_database.append(channel_id) # list with indexes matching those of the names
channel_names_from_database.append(channel_name) # list with indexes matching those of the id's
# Create a dropdown selection box
selected_option = st.selectbox(
'Select a channel to export',
channel_names_from_database
)
# return the channel_id directly instead of the channel_name
index = channel_names_from_database.index(
selected_option) # check the index in the list for the chosen channel name
return channel_ids_from_database[index], \
channel_names_from_database[index] # return the channel ID and name
def export_data(chan_id, chan_name):
query = "SELECT UserName, Message FROM Posts INNER JOIN Users " \
"ON Posts.UserId = Users.Id WHERE ChannelId = '" + chan_id + "' ORDER BY Posts.CreateAt"
# Fetch rows into lists
posts = []
for row in query_db(query):
posts.append(row)
# Create a download button for the CSV file
current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
export_to_csv(posts, current_datetime + "_" + string_to_filename(chan_name) + ".csv")
# display results
st.table(posts)
def get_base64(bin_file):
# Decode binary files like images
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
def show_background():
# decode the binary
bin_str = get_base64("img/background.jpg")
# set the style element
page_bg_img = '''
<style>
.stApp {
background-image: url("data:image/png;base64,%s");
background-size: cover;
}
</style>
''' % bin_str
# render the background
st.markdown(page_bg_img, unsafe_allow_html=True)