• Stars
    star
    190
  • Rank 203,739 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📕 Python library and CSS theme to generate PDF reports from HTML/Pug

PDF_Reports

PDF Reports Logo

Travis CI build status https://coveralls.io/repos/github/Edinburgh-Genome-Foundry/pdf_reports/badge.svg?branch=master

PDF Reports (complete documentation here) is a Python library to create nice-looking PDF reports from HTML or Pug templates. It features modern-looking components (via the Semantic UI framework) and provides routines to embed tables or plots in the documents.

Note that only Python 3.x is officially supported, although with the right version of weasyprint the library can also run on 2.x.

Example of use

Your Pug template file template.pug may look like this (see a full example):

#sidebar I am the text in the sidebar.

h1 {{ title }}

.ui.piled.segment
  p Oh hi there ! I am some text in a cool box.

Your Python code will be as follows:

from pdf_reports import pug_to_html, write_report
html = pug_to_html("template.pug", title="My report")
write_report(html, "example.pdf")

And your final result may look like this (PDF file):

https://github.com/Edinburgh-Genome-Foundry/pdf_reports/raw/master/screenshot.png

See also this example embedding some python code in the template to create figures and tables on the flight.

Other features

Preloading CSS and SCSS

PDF Reports provides a preload_stylesheet method which can be used to load and parse a CSS file. It also works with SCSS files (which will automatically be compiled to CSS) but this requires libsass installed (for instance via pip install libsass). Here is an example:

from pdf_reports import pug_to_html, write_report, preload_stylesheet

css = preload_stylesheet('style.scss')
html = pug_to_html("template.pug", title="My report", my_name='Zulko')
write_report(html, "example.pdf", extra_stylesheets=[css])

Using a ReportWriter

The ReportWriter class allows to define default templates, styles, and variable names. It can be used to avoid repeating yourself across your application:

from pdf_reports import ReportWriter

# DEFINE A WRITER WITH DEFAULT TEMPLATE AND VALUES
report_writer = ReportWriter(
    default_stylesheets=["style.css"],
    default_template="template.pug",
    title="My default title",
    version="0.1.2"
)

# THEN LATER IN YOUR CODE:
html = report_writer.pug_to_html(my_name="Zulko", my_organization="EGF")
report_writer.write_report(html, "example_reportwriter.pdf")

Markdown support

As a feature of PyPugJS, markdown is supported in the Pug templates.

div
  :markdown
    This is some markdown text. Here is a [link](http://example.com/).

    - this is a bullet point list
    - Second item
    - Etc.

PDF tools

Some useful functions for generating reports are available from inside the Pug templates under pdf_tools. For instance, pdf_tools.figure_data() to embed matplotlib images, or pdf_tools.dataframe_to_html() to turn Pandas dataframes into HTML, and style them nicely with Semantic UI. Have a look at the docs, or this example.

JupyterPDF

The JupyterPDF class eases report templates writing by embedding PDF files in Jupyter notebooks (using the browser's interactive PDF viewer).

from pdf_reports import JupyterPDF

# Build your PDF

# At the end of the notebook cell:
JupyterPDF("path_to_your.pdf")

Notes

The core of the library consists of just a few lines of Python, using pypugjs to parse Pug templates, optionally including stylesheets from the Semantic UI CSS framework, and finally calling weasyprint for PDF generation. Please refer to the Weasyprint documentation for the customization of templates. For instance, to customize the page margins and numbering the Weasyprint way, add this to your SCSS code:

@page {
    margin: 1cm 0 2cm 0cm;
    @bottom-center {
        content: "Page " counter(page) " / " counter(pages);
        font-family: 'Lato';
    }
}

Using Semantic UI implies that (1) the Lato font family should be installed on your machine, otherwise the results will look less good, and (2) the first time that write_pdf is called in a Python session, if using the default Semantic UI style, the parsing of the CSS will add a 3-second overhead to the function calls (but there will be no overhead for the next calls in that session).

Installation

You can install the library via PIP:

pip install pdf_reports

Alternatively, you can unzip the sources in a folder and type:

python setup.py install

Note: the package depends on the WeasyPrint Python package. If there are any issues, see installation instructions in the WeasyPrint documentation. The version is fixed to <=52 as not all GNU/Linux distributions have the latest Pango that is required by the latest WeasyPrint.

Note: on some Debian systems you may need to first install libffi-dev (apt install libffi-dev). The package name may be libffi-devel on some systems.

Note: on macOS, you may need to first install pango with: brew install pango

License = MIT

This open-source software project was originally written at the Edinburgh Genome Foundry by Zulko and released on Github under the MIT licence (Copyright 2018 Edinburgh Genome Foundry). Everyone is welcome to contribute !

More Repositories

1

DnaFeaturesViewer

👁️ Python library to plot DNA sequence features (e.g. from Genbank files)
Python
491
star
2

DnaChisel

✏️ A versatile DNA sequence optimizer
Python
176
star
3

Flametree

🔥 Python file and zip operations made easy
Python
147
star
4

Taskpacker

🎒 Simple schedule optimization library for Python
Python
135
star
5

blabel

🏷️ Python label/sticker PDF generation. HTML templates, built-in barcodes, qr codes, and other goodies
Python
135
star
6

Proglog

📝 Logs and progress bars manager for Python
Python
93
star
7

lala

🌎 Analyze and generate reports of web logs (NGINX)
Python
60
star
8

DnaCauldron

⚗️ Simple cloning simulator (Golden Gate etc.) for single and combinatorial assemblies
Python
44
star
9

sequenticon

👾 Generate identicons for DNA sequences with Python
Python
36
star
10

Plateo

🤖 Python biolab automation library: parsers, reports generators, picklists simulators, and more
Python
35
star
11

Primavera

🌸 Python library for primer-based verification of DNA assemblies: primer selection, data analyis, etc.
Python
32
star
12

codon-usage-tables

📊 Codon usage tables in code-friendly format + Python bindings
Python
31
star
13

Geneblocks

💠 Find common blocks and differences between DNA sequences
Python
28
star
14

crazydoc

Read DNA sequences from colourful Microsoft Word documents
Python
24
star
15

DnaWeaver

A route planner for DNA assembly
Python
22
star
16

Caravagene

🎨 Python library to plot multi-part genetic constructs
Python
21
star
17

genome_collector

🌠 Easily download genomes and build BLAST/Bowtie indexes in Python
Python
20
star
18

CAB

🚖 The friendly Computational App Boilerplate. Django + Vue.JS + Redis queues + NginX
Vue
20
star
19

CUBA

🏖️ The EGF Collection of Useful Bio Apps - Web demos of EGF software
Vue
19
star
20

BandWagon

🎺 Plot DNA digestion band patterns with Python
Python
17
star
21

BandWitch

💫 Computer-aided DNA assembly validation and identification from restriction digests.
Python
15
star
22

SBOL-Visual-CSS

➰ Draw genetic elements with HTML & CSS
HTML
15
star
23

genedom

Batch domestication of genetic parts with Python
Python
13
star
24

Sequeduct

Sequencing analysis pipeline
Nextflow
12
star
25

bioprinter

🖨️ Print pictures with living micro-organisms !
Python
12
star
26

Minotaor

An amino acid sequence annotator
Python
11
star
27

tatapov

🐾 DNA overhang misannealing data for Python
Python
9
star
28

GoldenHinges

🔗 Short overhangs design for DNA assembly
Python
9
star
29

zymp

✂️ Design compact restriction sites arrays (python utility)
Python
8
star
30

kappagate

🔮 Predict DNA assembly clone validity rates - powered by Kappa
Python
8
star
31

dab

👉 EGF Design and Build, the Foundry's DNA design ordering portal
Vue
8
star
32

egf-shared-documents

📚 Shared slideshows, courses, etc. from the EGF
HTML
7
star
33

easy_dna

🎒 Python library to read, write, edit DNA sequences
Python
6
star
34

icebreaker

❄️ Python API for the JBEI-ICE sample manager
Python
6
star
35

Examples

Collection of Python modules and Jupyter notebook examples
HTML
5
star
36

topkappy

🌔 Pythonic bindings for the Kappa model simulation language
Python
5
star
37

crecombio

A simple Cre, Flp and other site-specific recombination simulator
Python
4
star
38

HowTo

📒 Short opinionated recipes, mostly for us.
Python
4
star
39

genedeals

Data on different commercial offers for gene-sized DNA
4
star
40

igem-registry-downloader

Download the full iGEM database of parts
Python
3
star
41

saboteurs

💣 Identify elements impairing success accross group experiments.
Python
3
star
42

Ediacara

Python package for interpreting sequencing data of assembled DNA constructs (plasmids)
Python
3
star
43

DnaWeaver-online

A web app for DNA Weaver
Vue
2
star
44

trellab

Thin layer on top of pytrello for automating Trello organization-based tasks
Python
2
star
45

Polymera

Polymera is a Python package for representing ambiguous sequences written with complement alphabets.
Python
2
star
46

OT2Metclo

MSc project
Python
2
star
47

GeneAlloy

Python
2
star
48

Edinburgh-Genome-Foundry.github.io

📚 The EGF Software Website
HTML
1
star
49

tatapov_data

Data for the EGF Tatapov package
1
star
50

egf-codons-website

📚 Sources of the EGF Codons website, written with Vue.js
Vue
1
star
51

Overhang

Compendium of overhangs
Python
1
star
52

Plasmid_assessor

Plasmid assessment for Golden Gate cloning
Python
1
star
53

ImagesAnnotator

Simple web app to annotate images by hand - built with Vue.js
JavaScript
1
star
54

EGF_Docker_Jupyter

Docker Jupyter images with (almost) all EGF packages
Dockerfile
1
star