pybloqs.block package

Submodules

pybloqs.block.base module

class pybloqs.block.base.BaseBlock(title=None, title_level=3, title_wrap=False, width=None, height=None, inherit_cfg=True, styles=None, classes=(), anchor=None, **kwargs)[source]

Bases: object

Base class for all blocks. Provides infrastructure for rendering the block in an IPython Notebook or saving it to disk in HTML, PDF, PNG or JPG format.

container_tag = 'div'
data

Function required to support interactive IPython plotting.

Should not be used directly.

Returns:Data to be displayed
email(title='', recipients=('docs', ), footer_text=None, header_block=None, footer_block=None, from_address=None, cc=None, attachments=None, **kwargs)[source]

Send the rendered blocks as email. Each output format chosen will be added as an attachment.

Parameters:
  • title – title of the email
  • recipients – recipient of the email
  • fmt – One or more output formats that should be included as attachments. The following formats are supported: - HTML - PDF - PNG - JPG
  • body_block – The block to use as the email body. The default behavior is to use the current block.
  • footer_text – string to be used in place of the default footer text
  • from_address – sender of the message. Defaults to user name. Can be overwritten in .pybloqs.cfg with yaml format: ‘user_email_address: a@b.com
  • cc – cc recipient
  • kwargs – Optional arguments to pass to Block.save()
publish(name, *args, **kwargs)[source]

Publish the block so that others can access it.

Parameters:
  • name – Name to publish under. Can be a filename or a relative path.
  • args – Arguments to pass to Block.save.
  • kwargs – Keyword arguments to pass to Block.save.
Returns:

Path to the published block file.

render_html(pretty=True, static_output=False, header_block=None, footer_block=None, pdf_page_size='A4')[source]

Returns html output of the block :param pretty: Toggles pretty printing of the resulting HTML. Not applicable for non-HTML output. :return html-code of the block

resource_deps = []
save(filename=None, fmt=None, toc=False, pdf_zoom=1, pdf_page_size='A4', pdf_auto_shrink=True, pretty=True, orientation='Portrait', header_block=None, header_spacing=5, footer_block=None, footer_spacing=5, java_script_delay=200, **kwargs)[source]

Render and save the block. Depending on whether the filename or the format is provided, the content will either be written out to a file or returned as a string.

Parameters:
  • filename – Format will be based on the file extension. The following formats are supported: - HTML - PDF - PNG - JPG
  • fmt – Specifies the format of a temporary output file. When supplied, the filename parameter must be omitted.
  • toc – Toggles the generation of Table of Contents. Note: currently supported for PDF output only.
  • pdf_zoom – The zooming to apply when rendering the page to PDF.
  • pdf_page_size – The page size to use when rendering the page to PDF.
  • pdf_auto_shrink – Toggles auto-shrinking content to fit the desired page size.
  • pretty – Toggles pretty printing of the resulting HTML. Not applicable for non-HTML output.
Returns:

html filename

show(fmt='html', header_block=None, footer_block=None)[source]

Show the block in a browser.

Parameters:fmt – The format of the saved block. Supports the same output as Block.save
Returns:Path to the block file.
to_static()[source]
class pybloqs.block.base.HRule(title=None, title_level=3, title_wrap=False, width=None, height=None, inherit_cfg=True, styles=None, classes=(), anchor=None, **kwargs)[source]

Bases: pybloqs.block.base.BaseBlock

Draws a horizontal divider line.

pybloqs.block.colors module

pybloqs.block.colors.css_color(color)[source]

Create CSS color (rgb string or Hex string) from various inputs.

Digests:
  • tuple(float, float, float)
  • tuple(float, float, float, float)
  • list(float, float, float)
  • list(float, float, float, float)
  • “rgb(int, int, int)” (rgb string)
  • “rgba(int, int, int, int)” (rgba string)
  • “#0011FF” (Hex string)

Float colors are assumed from interval [0,1]. Int colots are assumed from interval [0,255].

pybloqs.block.colors.css_color_from_string(color_string)[source]
pybloqs.block.colors.css_color_from_tuple(color_tuple)[source]
pybloqs.block.colors.rgb_tuple_from_rgba(color_tuple)[source]

Convert rgba tuple to rgb tuple by converting color value explicitly.

pybloqs.block.convenience module

pybloqs.block.convenience.Block(contents=None, title=None, title_level=3, title_wrap=False, inherit_cfg=True, **kwargs)[source]

Constructs a composable layout element that will be rendered automatically by IPython Notebooks. It can also be saved in HTML, PDF, PNG or JPEG formats.

Content is handled as follows:

  • Lists, tuples and sets are written out into a grid layout, with a single column by default. Individual elements of the grid are parsed recursively.
  • DataFrames are written out as an interactive HTML table.
  • WidePanels are written out as a series of DataFrames along the Items axis. Each item in the panel will have the item name as a title, with the title level one below the current block title level.
  • Strings are written out in a raw format, preserving any HTML content in them.
  • Nested blocks are simply wrapped, unless there is more than one in which case the same logic applies as for lists, tuples and sets.
Parameters:
  • contents – Contents to put in a block.
  • title – Optional title of the block.
  • title_level – Optional title level (adjusts the size and TOC level), 1 being the biggest and 9 being the smallest.
  • title_wrap – Toggles whitespace wrapping of the title. (Default: False).
  • inherit_cfg – Optional. Set to False to ensure that the block does not inherit any parameters from parents.
  • cascade_cfg – Set to True to enable parmater cascading from this block. A value of False means that child blocks do not inherit parameters from this block.
  • kwargs – Optional styling arguments. The style keyword argument has special meaning in that it allows styling to be grouped as one argument. It is also useful in case a styling parameter name clashes with a standard block parameter.
Returns:

A block instance.

pybloqs.block.convenience.add_block_types(objects, block_cls)[source]

pybloqs.block.data_tables module

class pybloqs.block.data_tables.DataTablesCSSClass(paging=True, searching=True, info=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

create_table_level_css_class()[source]
class pybloqs.block.data_tables.DataTablesHTMLJinjaTableBlock(df, formatters=None, use_default_formatters=False, paging=True, searching=True, info=True, **kwargs)[source]

Bases: pybloqs.block.table.HTMLJinjaTableBlock

resource_deps = [<pybloqs.static.Css object>, <pybloqs.static.JScript object>, <pybloqs.static.JScript object>, <pybloqs.static.JScript object>]

pybloqs.block.image module

class pybloqs.block.image.ImgBlock(data, mime_type='png', width=None, height=None, img_style=None, **kwargs)[source]

Bases: pybloqs.block.base.BaseBlock

static from_file(img_file, **kwargs)[source]

Load an image block from a file.

Parameters:
  • img_file – File path or file-like object.
  • kwargs – Arguments to pass to the ImgBlock constructor.
Returns:

ImgBlock instance.

class pybloqs.block.image.PlotBlock(plot, close_plot=True, bbox_inches='tight', width=None, height=None, **kwargs)[source]

Bases: pybloqs.block.image.ImgBlock

pybloqs.block.image.set_plot_format(plot_format=None, plot_dpi=None)[source]

pybloqs.block.layout module

class pybloqs.block.layout.CompositeBlockMixin[source]

Bases: object

Mixin to support composite blocks. Must have a _contents attribute!

class pybloqs.block.layout.Flow(contents, cascade_cfg=True, **kwargs)[source]

Bases: pybloqs.block.layout.CompositeBlockMixin, pybloqs.block.base.BaseBlock

class pybloqs.block.layout.Grid(contents, cols=1, cascade_cfg=True, **kwargs)[source]

Bases: pybloqs.block.layout.CompositeBlockMixin, pybloqs.block.base.BaseBlock

class pybloqs.block.layout.HStack(contents, cascade_cfg=True, **kwargs)[source]

Bases: pybloqs.block.layout.Grid

class pybloqs.block.layout.VStack(contents, cascade_cfg=True, **kwargs)[source]

Bases: pybloqs.block.layout.CompositeBlockMixin, pybloqs.block.base.BaseBlock

pybloqs.block.table module

class pybloqs.block.table.HTMLJinjaTableBlock(df, formatters=None, use_default_formatters=True, **kwargs)[source]

Bases: pybloqs.block.base.BaseBlock

class FormatterData(cell, row_name, column_name, df)

Bases: tuple

cell

Alias for field number 0

column_name

Alias for field number 2

df

Alias for field number 3

row_name

Alias for field number 1

HTMLJinjaTableBlock.create_cell_level_css(cell, row_name, column_name)[source]
HTMLJinjaTableBlock.create_row_level_css(row_name, row)[source]
HTMLJinjaTableBlock.create_table_level_css()[source]
HTMLJinjaTableBlock.create_table_level_css_class()[source]
HTMLJinjaTableBlock.create_thead_level_css()[source]
HTMLJinjaTableBlock.insert_additional_html()[source]
HTMLJinjaTableBlock.modify_cell_content(cell, row_name, column_name)[source]

pybloqs.block.table_formatters module

class pybloqs.block.table_formatters.FmtAddCellBorder(each=None, left=None, right=None, top=None, bottom=None, length_unit='px', style='solid', color=(0.12, 0.2, 0.49), rows=None, columns=None, apply_to_header_and_index=False)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Add border on around table cells. For each side with border, specify border width, ‘each’ takes precedence.

class pybloqs.block.table_formatters.FmtAddCellPadding(left=None, right=None, top=None, bottom=None, length_unit='px', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Add space on cell sides.

class pybloqs.block.table_formatters.FmtAlignCellContents(alignment='center', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Align cell contents. Possible alignment values: left, center, right.

class pybloqs.block.table_formatters.FmtAlignTable(alignment, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set table alignment on page. Possible alignment paramters: left, center, right.

class pybloqs.block.table_formatters.FmtAppendTotalsColumn(column_name='Total', operator=<function sum>, bold=True, background_color=(0.9, 0.9, 0.9), font_color=None, total_rows=None)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Add another column at table right edge containing sum/mean/etc. of specified columns

class pybloqs.block.table_formatters.FmtAppendTotalsRow(row_name='Total', operator=<function sum>, bold=True, background_color=(0.9, 0.9, 0.9), font_color=None, total_columns=None, hline_color=(0.12, 0.2, 0.49), hline_style='1px solid')[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Add another row at table bottom containing sum/mean/etc. of specified columns

class pybloqs.block.table_formatters.FmtBold(rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set bold font in table cells.

class pybloqs.block.table_formatters.FmtColumnMultiIndexBasic(cell_css=None, index_col_css=None)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Fine grained control of CSS output for column multi-index.

Parameters:
  • cell_csslist of lists Per cell CSS tags arranged as one list per row and then a list of all row lists. Please note: Number of cells is variable in cells above the lowest line as repeating cell values are interpreted . In the last line, number of cells equals the number of columns in DataFrame. An entry in cell_css can be None, if no CSS should be set for this cell.
  • index_col_csslist of strings Per cell CSS tags for each cell in index column.
class pybloqs.block.table_formatters.FmtColumnMultiIndexRows(row_css=None, index_col_css=None)[source]

Bases: pybloqs.block.table_formatters.FmtColumnMultiIndexBasic

Apply one CSS string for each cell in each row of column MultiIndex.

Parameters:
  • row_csslist of strings List with a CSS string which is applied to each cell in row. By default, centers each line except the last, which is right aligned.
  • index_col_csslist of strings Per cell CSS tags for each cell in index column.
class pybloqs.block.table_formatters.FmtDDMMMYYYY(rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtDates

Change cell value from date formats to string, format as e.g. 01-Dec-2001. Uses FmtDates.

class pybloqs.block.table_formatters.FmtDates(fmt_string, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtToString

Apply formatting string if cell content is date. Changes cell content from date to string.

class pybloqs.block.table_formatters.FmtDecimals(n, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtNumbers

Change cell value from float to string and apply number format to n decimals. Uses FmtNumbers.

class pybloqs.block.table_formatters.FmtExpandMultiIndex(total_columns=None, operator=<function sum>, bold=True, indent_px=20, hline_color=(0.12, 0.2, 0.49), level_background_colors=None)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Expand multi-indexed table into single index table, grouping by index level with optional sum/mean/etc.

class pybloqs.block.table_formatters.FmtFontFamily(font_family='Arial, Helvetica, sans-serif', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set the font family, see below for suggested family combinations. Please note the use of single and doule quotes.

Possible font-family strings:
“Arial, Helvetica, sans-serif” “‘Times New Roman’, Times, serif” “‘Comic Sans MS’, cursive, sans-serif” “‘Courier New’, Courier, monospace”
class pybloqs.block.table_formatters.FmtFontsize(fontsize, unit='px', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set fontsize in table cells.

class pybloqs.block.table_formatters.FmtHeader(fixed_width='100%', index_width=None, column_width=None, rotate_deg=0, top_padding=None, no_wrap=True, columns=None)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set various header formatting. Fixes table width.

class pybloqs.block.table_formatters.FmtHeatmap(min_color=(0.9, 0.5, 0.5), max_color=(0.5, 0.9, 0.5), threshold=0.0, axis=None, rows=None, columns=None, apply_to_header_and_index=False, cache=None)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Color cell background by value. For column-wise or row-wise min/max coloring, set axis parameter.

class pybloqs.block.table_formatters.FmtHideCells(rows=None, columns=None, apply_to_header_and_index=True, use_visibility=False)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Prevents rows and columns from being displayed, but they will still influence e.g. sum operations.

class pybloqs.block.table_formatters.FmtHighlightBackground(color=(0.651, 0.024, 0.157), rows=None, columns=None, apply_to_header_and_index=False)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set background color of selected cells

class pybloqs.block.table_formatters.FmtHighlightText(bold=True, italic=True, font_color=(0.204, 0.541, 0.741), rows=None, columns=None, apply_to_header_and_index=False)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Change font formatting to highlight text in cell.

class pybloqs.block.table_formatters.FmtMultiplyCellValue(d, suffix, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Base class for dividing cell value by some number and adding suffix to columns

class pybloqs.block.table_formatters.FmtNumbers(fmt_string, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtToString

Apply formatting string if cell content is number. Changes cell content from number to string.

class pybloqs.block.table_formatters.FmtPageBreak(no_break=True, repeat_header=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Various settings controlling printed page output. Please see notes below regarding repeated headers.

Webkit has a bug so it will not repeat table headers after page-break. This is a bug that has not been fixed for over 6 years and affects, e.g. Chrome. wkhtmltopdf is webkit based, but fixes this issue separately. This fix relies in part on a patched version of qt. The fix does not work for rotated headers, which are not repeated.

Handling of repeated headers (including rotated ones) is fine, e.g. in Firefox, where repeat_header option works as expected.

class pybloqs.block.table_formatters.FmtPercent(n_decimals, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtNumbers

Change cell value from float to string and apply number format to percent with n decimals. Uses FmtNumbers.

class pybloqs.block.table_formatters.FmtReplaceNaN(value='', replace_inf=True, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Replace NaN and Inf (if replace_inf is True) values.

class pybloqs.block.table_formatters.FmtStripeBackground(first_color=(0.9, 0.9, 0.9), second_color=(1.0, 1.0, 1.0), header_color=(1.0, 1.0, 1.0), rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Set altenating cell backgroup colors.

class pybloqs.block.table_formatters.FmtThousandSeparator(n_decimals=0, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtNumbers

Change cell value from float to string, format to n-decimals and separate thousands iwth ‘,’. Uses FmtNumbers.

class pybloqs.block.table_formatters.FmtToString(fmt_string, rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.TableFormatter

Apply formatting string. Changes cell content to string.

class pybloqs.block.table_formatters.FmtValueToBps(suffix='', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtMultiplyCellValue

Divide cell values by 1e4 and add suffix to column name (if it is a string).

class pybloqs.block.table_formatters.FmtValueToMillion(suffix='', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtMultiplyCellValue

Divide cell values by 1e6 and add suffix to column name (if it is a string).

class pybloqs.block.table_formatters.FmtValueToPercent(suffix='', rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtMultiplyCellValue

Divide cell values by 1e2 and add suffix to column name (if it is a string).

class pybloqs.block.table_formatters.FmtYYYYMMDD(rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: pybloqs.block.table_formatters.FmtDates

Change cell value from date formats to string, format as e.g. 2001-12-01. Uses FmtDates.

class pybloqs.block.table_formatters.TableFormatter(rows=None, columns=None, apply_to_header_and_index=True)[source]

Bases: object

Base class for table formatters.

Consists of hook functions, which are called by HTMLJinjaTableBlock and a few helper functions, which may be useful within the hook functions.

Provides the following hooks, which can be implemented by derived classes:

_modify_dataframe()
Changes to the underlying dataframe, e.g. adding or removing rows or columns
_create_table_level_css()
Provides CSS styles to the <table> HTML tag.
_modify_cell_content()
Is applied to cell value, e.g. divide by 1e6 or convert number to string with specific number format.
_create_cell_level_css()
Provides CSS styles to all <th> and <td> HTML tags.
create_cell_level_css(data)[source]

Formatting on CSS level, e.g. colors, borders, etc.

create_row_level_css(data)[source]

Formatting on CSS level, e.g. colors, borders, etc.

create_table_level_css()[source]

Formatting on html-table level

create_table_level_css_class()[source]

CSS class of table

create_thead_level_css()[source]

Formatting on html-thead level

insert_additional_html()[source]

Inserts additional html (or java-script) before <table>.

modify_cell_content(data)[source]

Formatting for cell values, e.g. number formats

modify_dataframe(df)[source]

Changes the underlying dataframe.

pybloqs.block.text module

Module for blocks with text-only content

class pybloqs.block.text.Markdown(contents, dedent=True, **kwargs)[source]

Bases: pybloqs.block.text.Raw

Renders Markdown into HTML content.

encoding = 'UTF-8'
class pybloqs.block.text.Pre(contents, dedent=True, **kwargs)[source]

Bases: pybloqs.block.text.Raw

Renders the content with a fixed-width font, preserving whitespace.

container_tag = 'pre'
class pybloqs.block.text.Raw(contents, dedent=True, **kwargs)[source]

Bases: pybloqs.block.base.BaseBlock

class pybloqs.block.text.Span(contents, dedent=True, **kwargs)[source]

Bases: pybloqs.block.text.Raw

Renders a piece of text with formatting.

container_tag = 'span'

pybloqs.block.wrap module

class pybloqs.block.wrap.Box(contents, **kwargs)[source]

Bases: pybloqs.block.base.BaseBlock

class pybloqs.block.wrap.Paragraph(contents, **kwargs)[source]

Bases: pybloqs.block.wrap.Box

Wraps the content in a paragraph.

container_tag = 'p'

Module contents