Basic Printing
Printing to an HTML File
To create any HTML report, core printing functionality of Pyreball must be used.
The basic approach is to use print()
function, which can be used to print
values into the output HTML file.
Simple usage is demonstrated in the following script:
Which just adds the text into the HTML document:
It is possible to pass multiple arguments, each of which is converted to a string if necessary.
The following example encloses several values into a <div>
element.
The previous example can be reproduced with a shortcut
function print_div()
:
print_div()
function combines
print()
and div()
function.
div()
is one of the text util functions, which are demonstrated in
Text Utils
chapter.
Adding a Title
To add a title to the page, use set_title()
function.
import pyreball as pb
pb.set_title("Example Page")
pb.print("This text was not artificially generated ;)")
It creates a <h1>
title at the top of the page and sets also the <title>
element with the same value.
Note that set_title()
does not need to be called at the top of the
script, but it is recommended to do so for better readability.
Adding Headings
Every report should be structured properly into sections and subsections.
This can be achieved simply by
using print_h1()
, ...,
print_h6()
functions.
import pyreball as pb
pb.print_h1("The First Chapter")
pb.print_h2("The First Section")
pb.print_h2("Another Section")
pb.print_h1("The Second Chapter")
pb.print_h2("Last Section")
As can be seen from the generated HTML document, these functions don't just wrap text into <h*>
elements.
Pyreball also creates a table of contents with links to individual headings.
Moreover, it adds a pilcrow sign to each heading to create an anchor.
The table of contents, as well as heading numbers can be turned off by using either Pyreball's CLI arguments or config file.
Title vs. Headings
The main difference between set_title()
and print_h1()
, ...,
print_h6()
functions is
that set_title()
can be applied anywhere and it always changes the
title at the very beginning of the document, whereas print_h1()
, ...,
print_h6()
functions create headings in order they are called.
Moreover, title created by set_title()
is not included in the table
of contents and it also sets the value of <title>
element.
Last but not least, when title is set, it is also used instead of the Table of Contents
heading,
see the example below.