Many fixes to the dashboard.

This commit is contained in:
Remy Moll
2021-01-03 00:33:24 +01:00
parent 8c14b546d9
commit 069c83e796
8 changed files with 270 additions and 180 deletions

View File

@@ -2,125 +2,193 @@ import dash
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import locale
locale.setlocale(locale.LC_TIME, "de_DE.utf8")
#from dash.dependencies import Input, Output
import datetime
import time
import xmltodict
import requests
import emoji
class DashBoard():
""""""
def __init__(self, host_ip):
def __init__(self, host_ip, prst):
## pre-sets
self.today = datetime.date.today().strftime("%A, der %d. %B %Y")
self.inter_margin = "1em"
self.persistence = prst
self.host_ip = host_ip
ex_css = [dbc.themes.BOOTSTRAP]
self.app = dash.Dash(__name__, external_stylesheets=ex_css)
self.set_layout()
self.app.run_server(host=host_ip)
def set_layout(self):
card_placeholder = dbc.Card(
[
#dbc.CardImg(src="/assets/ball_of_sun.jpg", top=True, bottom=False,
# title="Image by Kevin Dinkel", alt='Learn Dash Bootstrap Card Component'),
dbc.CardBody(
[
html.H4("Learn Dash with Charming Data", className="card-title"),
html.H6("Lesson 1:", className="card-subtitle"),
html.P(
"Choose the year you would like to see on the bubble chart. Test this is a very long text that should hopefully have a line break.",
className="card-text",
),
html.P(
"Choose the year you would like to see on the bubble chart.",
className="card-text",
),
html.P(
"Choose the year you would like to see on the bubble chart.",
className="card-text",
),
html.P(
"Choose the year you would like to see on the bubble chart.",
className="card-text",
),
dbc.Button("Still not live I guess?", color="primary"),
dbc.CardLink("GirlsWhoCode", href="https://girlswhocode.com/", target="_blank"),
]
),
],
color="dark", # https://bootswatch.com/default/ for more card colors
inverse=True, # change color of text (black or white)
outline=False, # True = remove the block colors from the background and header
)
card_weather = dbc.Card(
)
card_bot_stats = dbc.Card(
)
card_header = dbc.Card(
[ dbc.CardImg(src="static/header.jpg", top=True, bottom=False,
title="Header", alt='Header image'),
dbc.CardBody([html.H4(self.today, className="card-title")]),
],
color="dark",
style = {"width" : "100%", "margin-bottom":self.inter_margin},
inverse=True,
)
card_shopping_list = dbc.Card(
[
dbc.CardBody([
html.H4("Shopping list:", className="card-title"),
# html.P("What was India's life expectancy in 1952?", className="card-text"),
dbc.ListGroup(
[
dbc.ListGroupItem("test"),
dbc.ListGroupItem("B. 37 years"),
dbc.ListGroupItem("C. 49 years"),
], flush=False)
]),
],
color="dark",
style = {"width" : "100%", "margin-bottom":self.inter_margin},
inverse=True,
)
self.app.layout = html.Div([
html.Div(
className = "content",
style={"padding":self.inter_margin},
children = [
# dbc.Row([dbc.Col(card_main, width=3),
# dbc.Col(card_question, width=3)], justify="around"), # justify="start", "center", "end", "between", "around"
card_header,
dbc.CardColumns([
card_shopping_list,
card_placeholder,
card_placeholder,
card_placeholder,
card_placeholder,
card_placeholder,
]),
])
html.Div(id = 'layout-update', className = "content", style={"padding":self.inter_margin},),
dcc.Interval(
id='interval-component',
interval=3600*1000, # in milliseconds
n_intervals=0
)
])
@self.app.callback(Output('layout-update','children'), Input('interval-component','n_intervals'))
def update_layout(n):
print("REFRESH")
kids = [
self.card_header(),
dbc.CardColumns([
*self.cards_lists(),
self.card_news(),
self.card_xkcd(),
self.card_weather()
])
]
return kids
#[card_header, dbc.CardColumns([card_shopping_list,card_placeholder,card_placeholder,card_placeholder,card_placeholder,card_placeholder])]
def launch_dashboard(self):
self.app.run_server(host=self.host_ip)#, debug=True)
def card_header(self):
today = datetime.date.today().strftime("%A, der %d. %B %Y")
card = dbc.Card(
[ dbc.CardImg(src="static/header.jpg", top=True, bottom=False,
title="Header", alt='Header image'),
dbc.CardBody([html.H3(today, className="card-title")]),
],
color="dark",
style = {"width" : "100%", "margin-bottom":self.inter_margin},
inverse=True,
)
return card
def cards_lists(self):
ret = []
for l in self.persistence["global"]["lists"].keys():
l_content = self.persistence["global"]["lists"][l]
html_content = [dbc.ListGroupItem(t) for t in l_content]
card = dbc.Card(
[
dbc.CardBody([
html.H4("Liste '" + l + "':", className="card-title"),
dbc.ListGroup(html_content, flush=True, style={"color":"black"})
]),
],
color="dark",
inverse=True,
)
ret.append(card)
return ret
# def card_bot_stats(self):
# return card
def card_weather(self):
try:
body = [html.H4("Wetter", className="card-title")]
content = self.bot.weather.show_weather([47.3769, 8.5417]) # still zürich
wt = content.pop(0)
body.append(html.Span(children=[
html.H6("Jetzt: " + wt["short"]),
html.P(emoji.emojize(":thermometer: ") + str(wt["temps"][0]) + "°")
]))
days = ["Montag", "Dienstag", "Miitwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"]
today = datetime.datetime.today().weekday()
for i, day in enumerate(content):
tmp = []
if i == 0:
tmp.append(html.H6("Heute: "+ day["short"]))
else:
tmp.append(html.H6(days[(today + i + 1) % 7] + ": " + day["short"]))
tmp.append(html.P(emoji.emojize(":thermometer: :fast_down_button: " + str(day["temps"][0]) + "° , :thermometer: :fast_up_button: " + str(day["temps"][1]) + "°")))
body.append(html.Span(children=tmp))
card = dbc.Card(
[dbc.CardBody(body)],
color="dark",
inverse=True,
)
except:
card = card = dbc.Card([
dbc.CardBody([
html.H4("Could not load WEATHER", className="card-title"),
])
],
color="dark",
inverse=True,
)
return card
def card_news(self):
try:
card = dbc.Card([
dbc.CardBody([html.Iframe(src="https://nzz.ch", style={"border":"none", "min-height":"30em", "width":"100%"})])
],
color="dark",
inverse=True,
)
except:
card = card = dbc.Card([
dbc.CardBody([
html.H4("Could not load NEWS", className="card-title"),
])
],
color="dark",
inverse=True,
)
return card
def card_xkcd(self):
try:
xml = requests.get("https://xkcd.com/atom.xml").content
feed = xmltodict.parse(xml)
title = feed["feed"]["entry"][0]["title"]
img = feed["feed"]["entry"][0]["summary"]["#text"]
i1 = img.find('"') +1
i2 = img.find('"', i1+1)
i3 = img.find('"', i2+1) + 1
i4 = img.find('"', i3+1)
img_src = img[i1:i2]
img_alt = img[i3:i4]
card = dbc.Card([
dbc.CardBody([
html.H4(title, className="card-title"),
html.Img(src=img_src, style={"width":"100%"}),
html.P(img_alt)
])
],
color="dark",
inverse=True,
)
except:
card = card = dbc.Card([
dbc.CardBody([
html.H4("Could not load XKCD", className="card-title"),
])
],
color="dark",
inverse=True,
)
return card
if __name__ == "__main__":
test = DashBoard(host_ip="0.0.0.0")
test = DashBoard(host_ip="0.0.0.0")