{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Composable Blocks & Interactive Charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is a simple illustration of the python API for Blocks and Highcharts interactive charts.\n", "\n", "**What are Blocks?**\n", "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.\n", "\n", "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.\n", "\n", "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.\n", "\n", "This notebook aims to showcase the functionality and offers some examples to help people get started." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Imports & Data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "import numpy as np\n", "import pandas as pd\n", "import pandas.util.testing as pt\n", "from datetime import datetime\n", "\n", "import pybloqs as p" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame((np.random.rand(200, 4)-0.5)/10,\n", " columns=list(\"ABCD\"),\n", " index=pd.date_range(datetime(2000,1,1), periods=200))\n", "\n", "df_cr = (df + 1).cumprod()\n", "\n", "a = df_cr.A\n", "b = df_cr.B\n", "c = df_cr.C\n", "c.name = \"C\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using Blocks\n", "Obligatory \"Hello World!\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " Hello World!\n", "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block(\"Hello World!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Play around with alignment" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " Hello World!\n", "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block(\"Hello World!\", h_align=\"left\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding a title" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "

\n", " Announcement\n", "

\n", " Hello World!\n", "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block(\"Hello World!\", title=\"Announcement\", h_align=\"left\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing out dataframes" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " A\n", " \n", " B\n", " \n", " C\n", " \n", " D\n", "
\n", " 2000-01-01 00:00:00\n", " \n", " 0.03\n", " \n", " -0.02\n", " \n", " -0.01\n", " \n", " -0.04\n", "
\n", " 2000-01-02 00:00:00\n", " \n", " 0.01\n", " \n", " 0.02\n", " \n", " 0.02\n", " \n", " -0.05\n", "
\n", " 2000-01-03 00:00:00\n", " \n", " 0.00\n", " \n", " -0.04\n", " \n", " -0.02\n", " \n", " -0.03\n", "
\n", " 2000-01-04 00:00:00\n", " \n", " -0.02\n", " \n", " 0.04\n", " \n", " 0.01\n", " \n", " -0.03\n", "
\n", " 2000-01-05 00:00:00\n", " \n", " -0.05\n", " \n", " 0.04\n", " \n", " -0.04\n", " \n", " -0.02\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block(df.head())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing out matplotlib plots" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n", "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block(df.A.plot())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Raw HTML output" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n", " this text is bold\n", " \n", "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block(\"this text is bold\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Composing blocks" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "

\n", " Announcement\n", "

\n", " Hello World!\n", "
\n", "
\n", "
\n", "
\n", " \n", " this text is bold\n", " \n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.VStack([p.Block(\"Hello World!\", title=\"Announcement\"), p.Block(\"this text is bold\")])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In most cases, one does not need to explicitly wrap elements in blocks" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", " Block 0\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 1\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 2\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 3\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 4\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 5\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 6\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 7\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block([\"Block %s\" % i for i in range(8)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Splitting composite blocks into columns" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", " Block 0\n", "
\n", "
\n", "
\n", "
\n", " Block 1\n", "
\n", "
\n", "
\n", "
\n", " Block 2\n", "
\n", "
\n", "
\n", "
\n", " Block 3\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 4\n", "
\n", "
\n", "
\n", "
\n", " Block 5\n", "
\n", "
\n", "
\n", "
\n", " Block 6\n", "
\n", "
\n", "
\n", "
\n", " Block 7\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block([\"Block %s\" % i for i in range(8)], cols=4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "
\n", " Block 0\n", "
\n", "
\n", "
\n", "
\n", " Block 1\n", "
\n", "
\n", "
\n", "
\n", " Block 2\n", "
\n", "
\n", "
\n", "
\n", " Block 3\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " Block 4\n", "
\n", "
\n", "
\n", "
\n", " Block 5\n", "
\n", "
\n", "
\n", "
\n", " Block 6\n", "
\n", "
\n", "
\n", "
\n", " Block 7\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block([\"Block %s\" % i for i in range(8)], cols=4, text_align=\"right\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using specific block types is simple as well. As an example - the Paragraph block:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "

\n", "

\n", " First paragraph.\n", "
\n", "

\n", "
\n", "
\n", "
\n", "
\n", "

\n", "

\n", " Second paragraph.\n", "
\n", "

\n", "
\n", "
\n", "
\n", "
\n", "

\n", "

\n", " Third paragraph.\n", "
\n", "

\n", "
\n", "
\n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Block([p.Paragraph(\"First paragraph.\"),\n", " p.Paragraph(\"Second paragraph.\"),\n", " p.Paragraph(\"Third paragraph.\")], text_align=\"right\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Pre block preserves whitespace formatting and is rendered using a fixed width font. Useful for rendering code-like text." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n",
       "some:\n",
       "  example:\n",
       "    yaml: [1,2,3]\n",
       "  data: \"text\"\n",
       "
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.Pre(\"\"\"\n", "some:\n", " example:\n", " yaml: [1,2,3]\n", " data: \"text\"\n", "\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " THIS HERE TEXT SHOULD LOOK LIKE SHOUTING!\n", "
" ], "text/plain": [ "<__main__.Capitalize at 0x7f4f85e1e9b0>" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Capitalize(p.Raw):\n", " def __init__(self, contents, **kwargs):\n", " # Stringify and capitalize\n", " contents = str(contents).upper()\n", "\n", " super(Capitalize, self).__init__(contents, **kwargs)\n", " \n", "Capitalize(\"this here text should look like shouting!\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please create ~/.pybloqs.cfg with entry for 'smtp_server'. See README.md and pybloqs/config.py for details.\n" ] } ], "source": [ "# Emails a block (or a report consisting of many blocks). The emailing is independent of previous reports being saved (e.g. there is no need to call save\n", "# before emailing).\n", "from smtplib import SMTPServerDisconnected\n", "\n", "try:\n", " p.Block('').email()\n", "except SMTPServerDisconnected:\n", " print(\"Please create ~/.pybloqs.cfg with entry for 'smtp_server'. See README.md and pybloqs/config.py for details.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Page break" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['wkhtmltopdf', '--no-stop-slow-scripts', '--debug-javascript', '--javascript-delay', '200', '--page-size', 'A4', '--orientation', 'Portrait', '--enable-smart-shrinking', '/tmp/69e57e72c8.html', 'two_page_report.pdf'] returned:\n", "\n", "None\n" ] }, { "data": { "text/plain": [ "'two_page_report.pdf'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blocks = [p.Block(\"First page\", styles={\"page-break-after\": \"always\"}),\n", " p.Block(\"Second page\")]\n", "r = p.VStack(blocks)\n", "r.save(\"two_page_report.pdf\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }