Build a Curated Report

A Report assembles native tables, charts, profile components, narrative text, and custom HTML into one HTML document.

Combine Outputs

import polarstory as ps

p = ps.profile('cohort.parquet')
report = ps.Report('Cohort exploration', subtitle='Descriptive results')

with report.section('Demographics'):
    report.add(p.summary('age'))
    report.add(p.histogram('age'))

with report.section('Encounter types'):
    report.add(p.frequency('enctype'))

report.write_html('cohort_report.html')

Add a complete variable profile

report = ps.Report('Selected variables')
report.add(p.variable('age'))
report.write_html('variables.html')

p.variable(name) is a VariableProfile; Report.add() expands it into its available summary and chart components.

Add your own polars summary

frequency = ps.stats.frequency('cohort.parquet', 'enctype')
report.add_table(frequency, title='Encounter type',
                 notes='Counts are of encounters, not distinct patients.')

Report.add_table() accepts eager or lazy polars tables. The default output limit is 2,000 rows to avoid silently generating enormous reports. Report.add_chart() accepts Matplotlib, Altair, and plotnine charts.

Add Formatted Tables with Great Tables

Great Tables provides additional control over column labels, number formatting, headers, colors, and footnotes. Install it separately when needed:

pip install great-tables
from great_tables import GT

frequency = ps.stats.frequency('cohort.parquet', 'enctype')
table = (
    GT(frequency)
    .tab_header(title='Encounter types')
    .opt_row_striping()
)
report.add_great_table(table)

Use Report.add_table() for native polars tables and Report.add_great_table() for tables formatted with Great Tables. Formatting is configured through Great Tables. An optional report-level title, caption, notes, or id can also be supplied. The native table row limit does not apply to Great Tables.

Add Text and Custom HTML

Use Report.add_text() for ordinary narrative text. HTML characters are escaped.

report.add_text(
    'The results describe encounters rather than unique patients.',
    title='Interpretation',
)

Use Report.add_html() to insert trusted HTML fragments without escaping or modification.

report.add_html('''
    <blockquote>
        <strong>Important:</strong>
        These results are preliminary.
    </blockquote>
''')

HTML can also be generated by an external library, for example report.add_html(table.as_raw_html()). Only insert trusted HTML, not untrusted input or a complete HTML document.

Choose a report component

Method Use
add() Existing polarstory results
add_table() Native polars tables
add_table1() Descriptive cohort characteristics
add_great_table() Formatted Great Tables tables
add_chart() Matplotlib, Altair, and plotnine charts
add_text() Plain narrative text
add_html() Custom or externally generated HTML
add_profile() Complete dataset profiles

See the Reports API for signatures and configuration options.

Organize and preview

report.add_text('Results are descriptive, not inferential.')
report.to_html()             # complete HTML as a string
report.write_html('study.html')

ReportItem.show() previews a single result. Report.remove(item_id) and Report.move(item_id, before=...) edit the ordering of collected results.

Printing and sharing

Native components use embedded SVG figures and print CSS, without requiring JavaScript or a server. Open the report in a browser and use Print → Save as PDF to export a PDF.

External html may introduce scripts, fonts, images, or other resource dependencies. Check the appearance and offline availability of external components before sharing or printing. Static great tables are recommended for printable, offline reports.

Disclosure caution: Small aggregate cells, unusual categories, and individual-level plots can still disclose information. The library does not guarantee safe sharing of protected health information.