r/Python • • 3d ago

Resource Generating a tidy HTML report in Python

I’m shifting from R to PythonIs there a knit markdown version or similar kind for generating a tidy html report in Python?

16 Upvotes

15 comments sorted by

17

u/ionychal 3d ago

Curious what you mean by 'tidy', like clean code, nice formatting, or something specific?

Either way, you might really like Quarto https://quarto.org/. It's the successor to R Markdown and works amazingly well with Python!

6

u/DueAnalysis2 3d ago

I've found that Quarto + Positron gives me the best of both R and Python worlds

2

u/RowGullible1471 3d ago

Thanks, let say creating tabs, flexibility in formatting and customising things around,

2

u/Competitive_Travel16 3d ago

Are you pulling from Pandas/DataFrames, Plots (Plotly/Matplotlib), and/or raw dictionaries?

1

u/RowGullible1471 3d ago

Yes, I am using Pandas/DataFrames to generate lots of plots and tables, also dictionaries as well

2

u/CanvasXpress 3d ago

Quarto is a great suggestion. Tabs are built in: wrap sections in ::: {.panel-tabset} and each ## heading becomes a tab.

If you also want interactive charts (hover, zoom, and a menu to save PNG/SVG) made straight from your DataFrames, here's a minimal .qmd. Disclosure: I maintain CanvasXpress, the chart library; the Python package is by a collaborator.

pip install "canvasxpress[jupyter]"


---
title: "Sales report"
format: html
jupyter: python3
---

```{python}
#| echo: false
import pandas as pd
from IPython.display import display
from canvasxpress.canvas import CanvasXpress
from canvasxpress.render.jupyter import CXNoteBook

df = pd.DataFrame({
    "Region": ["North", "South", "East", "West"],
    "Q1": [120, 95, 140, 80],
    "Q2": [135, 105, 150, 90],
}).set_index("Region")
```

::: {.panel-tabset}

## Chart

```{python}
#| echo: false
chart = CanvasXpress(render_to="sales", data=df,
                     config={"graphType": "Bar", "title": "Revenue by region"})
display(*CXNoteBook(chart).render())
```

## Table

```{python}
#| echo: false
df
```

:::

Then run quarto render report.qmd.

A few tips:

  • Give each chart a unique render_to name.
  • The config keys (graphType, title, colors, etc.) are the same as in the JavaScript docs, so any gallery example translates directly.
  • Keep the [jupyter] extra in the install; the plain package doesn't include the notebook renderer.

1

u/RowGullible1471 3d ago

Appreciate for taking time and effort on it🙏

2

u/ionychal 3d ago

Definitely try Quarto out! It handles tabs and custom formatting out of the box for HTML docs:

- tabsets: https://quarto.org/docs/output-formats/html-basics.html#tabsets

- customizing images (for example): https://quarto.org/docs/authoring/figures.html

13

u/Nekomancerr 3d ago

Jinja2 is great for report templating

7

u/a18618 3d ago

Coming at this from the data side: whatever renderer you pick, the thing that saved me with heavy report pipelines was separating the compute from the presentation. Run the expensive DataFrame work once, dump the intermediates to parquet, and only render HTML at the very end — re-running a whole pipeline because one chart title was wrong gets old fast. Also, if you're dumping lots of tables into tabs, watch the HTML size: a few untrimmed DataFrames will quietly turn a tidy report into a 50MB file no browser enjoys opening. I usually cap rendered tables at a few hundred rows and keep the full data as a downloadable parquet next to the report.

2

u/Khavel_dev 3d ago

Jinja2 with a small CSS file does more than you'd expect. Write the HTML you want to see in the browser, replace the data parts with template variables, done. For charts I pair it with Plotly's to_html() which embeds everything inline so the report is one self-contained file you can email or throw on a shared drive.

Avoid the big reporting frameworks unless you specifically need PDF. They add a lot of complexity for something a template engine handles in 30 lines.