-
Notifications
You must be signed in to change notification settings - Fork 55
/
streamlit_app.py
70 lines (46 loc) · 1.68 KB
/
streamlit_app.py
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
from datetime import datetime
import streamlit as st
from vega_datasets import data
from utils import chart, db
COMMENT_TEMPLATE_MD = """{} - {}
> {}"""
def space(num_lines=1):
"""Adds empty lines to the Streamlit app."""
for _ in range(num_lines):
st.write("")
st.set_page_config(layout="centered", page_icon="💬", page_title="Commenting app")
# Data visualisation part
st.title("💬 Commenting app")
source = data.stocks()
all_symbols = source.symbol.unique()
symbols = st.multiselect("Choose stocks to visualize", all_symbols, all_symbols[:3])
space(1)
source = source[source.symbol.isin(symbols)]
chart = chart.get_chart(source)
st.altair_chart(chart, use_container_width=True)
space(2)
# Comments part
conn = db.connect()
comments = db.collect(conn)
with st.expander("💬 Open comments"):
# Show comments
st.write("**Comments:**")
for index, entry in enumerate(comments.itertuples()):
st.markdown(COMMENT_TEMPLATE_MD.format(entry.name, entry.date, entry.comment))
is_last = index == len(comments) - 1
is_new = "just_posted" in st.session_state and is_last
if is_new:
st.success("☝️ Your comment was successfully posted.")
space(2)
# Insert comment
st.write("**Add your own comment:**")
form = st.form("comment")
name = form.text_input("Name")
comment = form.text_area("Comment")
submit = form.form_submit_button("Add comment")
if submit:
date = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
db.insert(conn, [[name, comment, date]])
if "just_posted" not in st.session_state:
st.session_state["just_posted"] = True
st.experimental_rerun()