"""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 p(text: str, style: str = "") -> str: """Wrap text into an HTML `p` tag. Parameters ---------- text : str Contents for HTML `p` tag style: str Div style 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, commment_text: str = "" ) -> 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'

' if comment_cell: comment = commment_text if commment_text else "No comment." html_text += f"""

{comment}

""" html_text += f"{htmlcode}
" return html_text def tab_clickable_and_save_evt() -> str: """Adds javascript code within HTML at the bottom that allows the interactivity with tabs. Parameters ---------- report_name : str Report name for the file to be saved Returns ------- str javascript code in HTML to process interactive tabs """ return """ """ def tablinks(tabs: List[str]) -> str: """Adds list of tabs/sections for the reports that are able to be clicked. For every 6 tabs we push them onto a new line. Parameters ---------- tabs : List[str] List of tabs/sections for the reports. Returns ------- str HTML code for interactive tabs """ htmlcode = '
' for idx, tab in enumerate(tabs): htmlcode += f"""""" if ((idx + 1) % 5) == 0: htmlcode += "
" htmlcode += "
" return htmlcode def header( openbb_img, floppy_disk_img, author, report_date, report_time, report_tz, title ) -> str: """Creates reports header Parameters ---------- openbb_img : str Image of OpenBB logo floppy_disk_img : str Image of floppy disk containing the save button author : str Name of author responsible by report report_date : str Date when report is run report_time : str Time when report is run report_tz : str Timezone associated with datetime of report being run title : str Title of the report Returns ------- str HTML code for interactive tabs """ try: with open(openbb_img, "rb") as image_file: openbb_image_encoded = base64.b64encode(image_file.read()) openbb_img = f""" OpenBB""" except Exception: openbb_img = "" try: with open(floppy_disk_img, "rb") as image_file: floppy_disk_encoded = base64.b64encode(image_file.read()) flask_disk_save = f"""
OpenBB
""" except Exception: flask_disk_save = "" return f""" OpenBB Terminal Report
{openbb_img}

Analyst: {author}

Date : {report_date}

Time : {report_time} {report_tz}


{title}

""" def add_external_fig(figloc: str, style: str = "") -> str: """Add external figure to HTML Parameters ---------- figloc : str Relative figure location style: str Div style Returns ------- str HTML code for figure """ try: with open(figloc, "rb") as image_file: img = base64.b64encode(image_file.read()).decode() return f'' except Exception: return ""