"""Widgets Helper Library.
A library of `ipywidgets` wrappers for notebook based reports and voila dashboards.
The library includes both python code and html/css/js elements that can be found in the
`./widgets` folder.
"""
import os
from typing import List
import base64
from jinja2 import Template
def price_card_stylesheet():
"""Load a default CSS stylesheet from file."""
with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "widgets", "style.css")
) as f:
style = f.read()
return style
def price_card(ticker: str, price: str, price_color: str = "neutral_color") -> str:
"""Prepare a styled HTML element of a 128 by 128 price card.
Parameters
----------
ticker : str
Instrument ticker for the price card
price : str
Instrument price as a string
price_color : str, optional
The color of the price. Accepts "up_color", "down_color" and default "neutral_color"
Returns
-------
str
HTML code as string
"""
with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "widgets", "card.j2")
) as f:
template = Template(f.read())
card = template.render(ticker=ticker, price=price, price_color=price_color)
return card
def html_report_stylesheet():
"""Load a default CSS stylesheet from file."""
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)), "widgets", "report.css"
)
) as f:
style = f.read()
return style
def html_report(title: str = "", stylesheet: str = "", body: str = "") -> str:
"""Prepare a styled HTML page element.
Parameters
----------
title : str, optional
Contents of the title tag, by default ""
stylesheet : str, optional
Contents of the stylesheet tag, by default ""
body : str, optional
Contents of the body tag, by default ""
Returns
-------
str
HTML code as string
"""
with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "widgets", "report.j2")
) as f:
template = Template(f.read())
return template.render(title=title, stylesheet=stylesheet, body=body)
def h(level: int, text: str) -> str:
"""Wrap text into an HTML `h` tag.
Parameters
----------
level : int
HTML `h` level tag
text : str
Contents for `h` level tag
Returns
-------
str
HTML code as string
"""
return f"
{text}
' def row(elements: List) -> str: """HTML code elements to add in a single row Parameters ---------- elements : List List of HTML code elements to add in a row Returns ------- str HTML code as string """ with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), "widgets", "row.j2") ) as f: template = Template(f.read()) return template.render(elements=elements) def kpi(thresholds: List[float], sentences: List[str], value: float) -> str: """Add new key performance indicator to main page of report Parameters ---------- thresholds : List[float] List of thresholds to take into account sentences : List[str] List of sentences to take into account. len(sentences) = len(thresholds)+1 value : float Current value for the KPI in question Returns ------- str HTML code as string """ if len(thresholds) == 1 and len(sentences) == 2: if value < thresholds[0]: return f'❌ {sentences[0]}. {value} < {thresholds[0]}
' return f'✅ {sentences[1]}. {value} > {thresholds[0]}
' if len(thresholds) == 2 and len(sentences) == 3: if value < thresholds[0]: return f'❌ {sentences[0]}. {value} < {thresholds[0]}
' if value > thresholds[1]: return f'✅ {sentences[2]}. {value} > {thresholds[1]}
' return f'🟡 {sentences[1]}. {thresholds[0]} < {value} < {thresholds[1]}
' print("Error. KPI condition is not correctly set") return "" def add_tab(title: str, htmlcode: str, comment_cell: bool = True) -> str: """Add new tab section for the report. By default adds an opinion editable box at the start. Parameters ---------- title : str Title associated with this tab / section htmlcode : str All HTML code contain within this section comment_cell : bool Comment cell Returns ------- str HTML code as string """ html_text = f'No comment.
""" html_text += f"{htmlcode}Analyst: {author}
Date : {report_date}
Time : {report_time} {report_tz}
{title}