-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit_app.py
113 lines (96 loc) · 4.45 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Important libraries
import json
import streamlit as st
from pathlib import Path
from streamlit_elements import elements, dashboard, mui, editor, media, lazy, sync, nivo
# Change page layout to wide
st.set_page_config(layout="wide")
# Create a side bar
with st.sidebar:
st.title("🗓️ #30DaysOfStreamlit")
st.header("Day 27 - Streamlit Elements")
st.write("Build a draggable and resizable dashboard with Streamlit Elements.")
st.write("---")
# Define URL for media player
media_url = st.text_input("Media URL", value="https://www.youtube.com/watch?v=vIQQR_yq-8I")
# Initialize default data for code editor and chart
if "data" not in st.session_state:
st.session_state.data = Path("data.json").read_text()
# Define a default dashboard layout
layout = [
# Editor item is positioned in coordinates x=0 and y=0, and takes 6/12 columns and has a height of 3
dashboard.Item("editor", 0, 0, 6, 3),
# Chart item is positioned in coordinates x=6 and y=0, and takes 6/12 columns and has a height of 3
dashboard.Item("chart", 6, 0, 6, 3),
# Media item is positioned in coordinates x=0 and y=3, and takes 6/12 columns and has a height of 4
dashboard.Item("media", 0, 2, 12, 4),
]
# Create a frame to display elements
with elements("demo"):
# Create a new dashboard with the layout specified above
with dashboard.Grid(layout, draggableHandle=".draggable"):
# First card, the code editor
with mui.Card(key="editor", sx={"display": "flex", "flexDirection": "column"}):
mui.CardHeader(title="Editor", className="draggable")
with mui.CardContent(sx={"flex": 1, "minHeight": 0}):
# Here is our Monaco code editor
editor.Monaco(
defaultValue=st.session_state.data,
language="json",
onChange=lazy(sync("data"))
)
with mui.CardActions:
# Create a button that fires a callback on click
mui.Button("Apply changes", onClick=sync())
# Second card, the Nivo Bump chart
with mui.Card(key="chart", sx={"display": "flex", "flexDirection": "column"}):
mui.CardHeader(title="Chart", className="draggable")
with mui.CardContent(sx={"flex": 1, "minHeight": 0}):
# This is where we will draw our Bump chart
nivo.Bump(
data=json.loads(st.session_state.data),
colors={ "scheme": "spectral" },
lineWidth=3,
activeLineWidth=6,
inactiveLineWidth=3,
inactiveOpacity=0.15,
pointSize=10,
activePointSize=16,
inactivePointSize=0,
pointColor={ "theme": "background" },
pointBorderWidth=3,
activePointBorderWidth=3,
pointBorderColor={ "from": "serie.color" },
axisTop={
"tickSize": 5,
"tickPadding": 5,
"tickRotation": 0,
"legend": "",
"legendPosition": "middle",
"legendOffset": -36
},
axisBottom={
"tickSize": 5,
"tickPadding": 5,
"tickRotation": 0,
"legend": "",
"legendPosition": "middle",
"legendOffset": 32
},
axisLeft={
"tickSize": 5,
"tickPadding": 5,
"tickRotation": 0,
"legend": "ranking",
"legendPosition": "middle",
"legendOffset": -40
},
margin={ "top": 40, "right": 100, "bottom": 40, "left": 60 },
axisRight=None,
)
# Third element of the dashboard, the Media player.
with mui.Card(key="media", sx={"display": "flex", "flexDirection": "column"}):
mui.CardHeader(title="Media Player", className="draggable")
with mui.CardContent(sx={"flex": 1, "minHeight": 0}):
# This element is powered by ReactPlayer
media.Player(url=media_url, width="100%", height="100%", controls=True)