Skip to Content
While25SDK docs
ExamplesApps and panels

Account desk as an HTML page

The same desk as blocks_dashboard.py, written as one HTML page on the platform's brand classes. Use `ui.html` when the arrangement needs markup of your own; the page has no script permission, so every value is computed here.

The same desk as blocks_dashboard.py, written as one HTML page on the platform's brand classes. Use ui.html when the arrangement needs markup of your own; the page has no script permission, so every value is computed here.

Shows: App, ui.layout(ui.html(...)), brand classes (.page .tiles .tile .card .num .c-profit .c-loss .c-dim), Money, absent funds.

View on GitHub

apps/html_dashboard.py
"""Account desk as an HTML page

The same desk as blocks_dashboard.py, written as one HTML page on the
platform's brand classes. Use `ui.html` when the arrangement needs markup of
your own; the page has no script permission, so every value is computed here.

Shows: App, ui.layout(ui.html(...)), brand classes (.page .tiles .tile .card .num .c-profit .c-loss .c-dim), Money, absent funds.
"""
import html

import while25
from while25 import App, on, ui

ui.panel("html_dashboard", "Desk (HTML)")
ui.layout(ui.html("view"))          # the only widget, so the page IS the panel


def money_cell(money):
    """(text, class) for a Money: the value with its sign colour, or a dash."""
    if money is None:
        return "—", "c-dim"
    v = money.value
    return f"{v:,.2f} {money.currency}", ("c-profit" if v >= 0 else "c-loss")


def margin_text(funds):
    """(text, class) for the available-margin tile. Words, never 0.00, when the
    broker states nothing (funds None) or no such figure (Alpaca)."""
    if funds is None:
        return "Unavailable", "c-dim"
    if funds["margin_available"] is None:
        return "Not stated", "c-dim"
    return money_cell(funds["margin_available"])


def row(pos: dict) -> str:
    text, cls = money_cell(pos.get("pnl"))
    qty = pos.get("quantity")
    return (f"<tr><td>{html.escape(str(pos.get('symbol', '—')))}</td>"
            f"<td class='num'>{'—' if qty is None else qty}</td>"
            f"<td class='num {cls}'>{text}</td></tr>")


def page(positions, funds) -> str:
    margin, margin_cls = margin_text(funds)
    rows = "".join(row(p) for p in positions[:12]) or (
        "<tr><td colspan='3' class='c-dim'>No open positions</td></tr>")
    return f"""
      <div class="page stack">
        <div class="header">
          <h3>Desk</h3>
          <span class="badge badge-active">live</span>
          <span class="stamp">refreshes every 5s</span>
        </div>
        <div class="tiles">
          <div class="tile"><div class="k">Open positions</div>
            <div class="v t-num">{len(positions)}</div></div>
          <div class="tile"><div class="k">Available margin</div>
            <div class="v t-num {margin_cls}">{margin}</div></div>
        </div>
        <div class="card">
          <table class="dense">
            <thead><tr><th>Symbol</th><th class="num">Qty</th><th class="num">P&amp;L</th></tr></thead>
            <tbody>{rows}</tbody>
          </table>
        </div>
      </div>"""


class HtmlDashboard(App):
    id = "html_dashboard"
    label = "Desk (HTML)"

    @on.schedule(every="5s")
    def draw(self, ctx):
        positions = while25.portfolio.positions() or []
        while25.ui.push("view", page(positions, while25.portfolio.funds()))