Composable Blocks & Interactive Charts

This notebook is a simple illustration of the python API for Blocks and Highcharts interactive charts.

What are Blocks? Blocks are composable layout elements for easily building HTML, PDF, PNG and JPG based reports. At the same time, all block constructs can be rendered in-line in IPython Notebooks (as will be shown later). This has practical benefits like a single backtest function that can be used for quick analysis during research work in a notebook, but also directly injected into more formal reports with having to fumble around with intermediate formats.

Practically all the functionality is based on HTML rendering. HTML is a declarative, tree based language that is easy to work with and fungible. Blocks are also declarative, composable into a tree and are meant to be dead simple. The match was thus quite natural.

The blocks do not try to match the power and precision of latex. Such an undertaking would be not only out of the scope of a simple library, but would mean the reinvention of latex with all the gnarliness that comes with it.

This notebook aims to showcase the functionality and offers some examples to help people get started.

Imports & Data

In [9]:
import numpy as np
import pandas as pd
import pandas.util.testing as pt
from datetime import datetime

# === Begin bits required for interactive plotting and reports ===
from pybloqs import *
from pybloqs.plot import *

interactive()
# === End interactive bits ===
Interactive mode initialized successfully
In [10]:
wp = pt.makePanel().ix[:, :, :2]

df = pd.DataFrame((np.random.rand(200, 4)-0.5)/10,
                  columns=list("ABCD"),
                  index=pd.date_range(datetime(2000,1,1), periods=200))

df_cr = (df + 1).cumprod()

a = df_cr.A
b = df_cr.B
c = df_cr.C
c.name = "C"

Using Blocks

Obligatory “Hello World!”

In [11]:
Block("Hello World!")
Out[11]:
Hello World!

Play around with alignment

In [12]:
Block("Hello World!", h_align="left")
Out[12]:
Hello World!

Adding a title

In [13]:
Block("Hello World!", title="Announcement", h_align="left")
Out[13]:

Announcement

Hello World!

Writing out dataframes

In [14]:
Block(df.head())
Out[14]:
A B C D
2000-01-01 00:00:00 -0.03 0.03 0.02 0.02
2000-01-02 00:00:00 -0.04 -0.02 -0.01 -0.01
2000-01-03 00:00:00 -0.01 0.04 -0.01 0.03
2000-01-04 00:00:00 0.00 0.03 -0.03 0.00
2000-01-05 00:00:00 0.03 -0.03 -0.03 -0.02

Writing out matplotlib plots

In [15]:
Block(df.A.plot())
Out[15]:

Raw HTML output

In [16]:
Block("<b>this text is bold</b>")
Out[16]:
this text is bold

Composing blocks

In [17]:
Block([Block("Hello World!", title="Announcement"), Block("<b>this text is bold</b>")])
Out[17]:

Announcement

Hello World!
this text is bold

In most cases, one does not need to explicitly wrap elements in blocks

In [18]:
Block(["Block %s" % i for i in xrange(8)])
Out[18]:
Block 0
Block 1
Block 2
Block 3
Block 4
Block 5
Block 6
Block 7

Splitting composite blocks into columns

In [19]:
Block(["Block %s" % i for i in xrange(8)], cols=4)
Out[19]:
Block 0
Block 1
Block 2
Block 3
Block 4
Block 5
Block 6
Block 7

Layout styling is cascading - styles will cascade from parent blocks to child blocks by default. This behavior can be disabled by setting inherit_cfg to false on the child blocks, or simply specifying the desired settings explicitly.

In [20]:
Block(["Block %s" % i for i in xrange(8)], cols=4, text_align="right")
Out[20]:
Block 0
Block 1
Block 2
Block 3
Block 4
Block 5
Block 6
Block 7

Using specific block types is simple as well. As an example - the Paragraph block:

In [21]:
Block([Paragraph("First paragraph."),
       Paragraph("Second paragraph."),
       Paragraph("Third paragraph.")], text_align="right")
Out[21]:

First paragraph.

Second paragraph.

Third paragraph.

The Pre block preserves whitespace formatting and is rendered using a fixed width font. Useful for rendering code-like text.

In [22]:
Pre("""
some:
  example:
    yaml: [1,2,3]
  data: "text"
""")
Out[22]:
some:
  example:
    yaml: [1,2,3]
  data: "text"

Creating custom blocks is trivial. For the majority of the cases, one can just inherit from the Container block, which has most of the plumbing already in place:

In [23]:
class Capitalize(Raw):
    def __init__(self, contents, **kwargs):
        # Stringify and capitalize
        contents = str(contents).upper()

        super(Capitalize, self).__init__(contents, **kwargs)

Capitalize("this here text should look like shouting!")
Out[23]:
THIS HERE TEXT SHOULD LOOK LIKE SHOUTING!

Simple line chart

When evaluated as the last expression in a Notebook Cell, the plot is automatically displayed inline.

Note how the plot name (hover over the line to see the little popup) is taken from the input data (if available).

In [24]:
Plot(a)
Out[24]:

Saving as interactive HTML

In [25]:
from pybloqs.plot import Plot
Plot(a).save("chart_sample.html")
Out[25]:
'chart_sample.html'

Scatter Plot

Regular scatter plot, with zooming on both the x and y axes.

In [26]:
Plot(df.values[:,:2], Scatter(Marker(enabled=True)), Chart(zoom_type="xy"))
Out[26]:

Bar Charts

Notice how when viewing all the data at once, the chart shows monthly data, yet zooming in reveals additional detail at up to daily resolution. This is accomplished by using a custom data grouping.

In [27]:
bar_grouping = DataGrouping(approximation="open", enabled=True, group_pixel_width=100)
In [28]:
# Bar chart from a dataframe
Plot(df, Column(bar_grouping))
Out[28]:

Stacked bar chart

Plot(df, Column(bar_grouping, stacking=”normal”))

In [29]:
# Composite bar chart from two separate plots.
s2 = Plot([Plot(a, Column(bar_grouping)),
               Plot(b, Column(bar_grouping))])
s2
Out[29]:

Comparing series in a dataframe

Plot the cumulative percentage difference between input series (or columns of a dataframe). The cumulative difference is always calculated from the start of the observation window. This results in intuitively correct behavior when zooming in or sliding the window, as the chart will dynamically recalculate the values. Incredibly useful for comparing model performance over time for example as one doesn’t need to manually normalize money curves for different periods.

In [30]:
s3 = Plot(df_cr,
              PlotOptions(Series(compare="percent")),
              TooltipPct(),
              YAxisPct())
s3
Out[30]:

Three series on separate side-by-side Y axes

In [31]:
s4 = Plot([Plot(a),
               Plot(b, YAxis(Title(text="B Axis"), opposite=True)),
               Plot(c, YAxis(Title(text="C Axis"), opposite=True, offset=40))])
s4
Out[31]:

Two series on separate subplots

In [32]:
s5 = Plot([Plot(a, Line(), YAxis(Title(text="a only"), height=150)),
               Plot(b, Column(), YAxis(Title(text="b only"),
                                                   top=200, height=100, offset=0))],
              Tooltip(value_decimals=2), height="400px")
s5
Out[32]:

Writing out multiple charts as HTML

In [33]:
b = Block([Block(pt.makeTimeDataFrame().tail(10), title="A table", title_level=1),
           Block([s2, s3], title="Side-by-side Plots", cols=2),
           Block(title="Full Width Plots", break_before="always"),
           Block(s4, title="Side by Side Axes"),
           Block(s5, title="Composite Plots")], title="Dynamic Reports")
In [34]:
b.save("charts_test.pdf")
b.save("charts_test.html")
['/users/is/dchrist/pyenvs/dev_RHEL7_pydev/bin/wkhtmltopdf', '--debug-javascript', '--page-size', 'A4', '--orientation', 'Portrait', '/tmp/xppdu10cy40s.html', 'charts_test.pdf'] returned:

None
Out[34]:
'charts_test.html'
In [35]:
# Emails the report. The emailing is independent of previous reports being saved (e.g. there is no need to call save
# before emailing).
from smtplib import SMTPServerDisconnected

try:
    b.email(fmt="html")
except SMTPServerDisconnected:
    print "Please create ~/.pybloqs.cfg with entry for smtp_server . See README.md and pybloqs/config.py for details."
Please create ~/.pybloqs.cfg with entry for smtp_server . See README.md and pybloqs/config.py for details.