Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jrycw/gt-fasthtml

Great Tables running in FastHTML
https://github.com/jrycw/gt-fasthtml

fasthtml pandas polars python table

Last synced: 20 days ago
JSON representation

Great Tables running in FastHTML

Awesome Lists containing this project

README

        

# gt-fasthtml

### Introduction

This guide will walk you through setting up a FastHTML application that renders a table using the `GT` library.

### Steps

1. **Create a FastHTML App Instance and Route**

Begin by creating an instance of the FastHTML app and defining a route:

```python
app, rt = fast_app()
```

2. **Create the `get` Endpoint**

In FastHTML, specialized HTML tags like `Div` and `Title` are used to directly describe HTML, rather than relying on templating languages like Jinja in traditional web apps.

First, define the `get` endpoint, which will generate the HTML when users visit the `/` route. The endpoint will include three HTML tags:

* `Title("FastHTML-GT Website")`: Generates a `` tag within the `` section.
* `Titled("Great Tables shown in FastHTML", style="text-align:center")`: Creates a `` tag and an `

` tag within the `` section.
* `NotStr(sza_tbl().as_raw_html())`: Renders a table generated by `GT.as_raw_html()` and wraps it with the `NotStr` callable provided by FastHTML, allowing you to reuse Python code that returns HTML.

```python
@cache
def get_sza():
return pl.from_pandas(sza)

@rt("/")
def get():
"""
https://docs.fastht.ml/tutorials/quickstart_for_web_devs.html#strings-and-conversion-order
"""
sza_pivot = (
get_sza()
.filter((pl.col("latitude") == "20") & (pl.col("tst") <= "1200"))
.select(pl.col("*").exclude("latitude"))
.drop_nulls()
.pivot(values="sza", index="month", on="tst", sort_columns=True)
)

sza_gt = (
GT(sza_pivot, rowname_col="month")
.data_color(
domain=[90, 0],
palette=["rebeccapurple", "white", "orange"],
na_color="white",
)
.tab_header(
title="Solar Zenith Angles from 05:30 to 12:00",
subtitle=html("Average monthly values at latitude of 20°N."),
)
.sub_missing(missing_text="")
)

return (
Title("FastHTML-GT Website"),
H1("Great Tables shown in FastHTML", style="text-align:center"),
NotStr(sza_gt.as_raw_html()),
)
```

3. **Run the Uvicorn Server**

Finally, start the Uvicorn server and open your browser to view the rendered table:

```bash
uvicorn main:app --reload
```

You should now see the table displayed in your browser at [http://127.0.0.1:8000](http://127.0.0.1:8000).

![table](https://raw.githubusercontent.com/jrycw/gt-fasthtml/refs/heads/master/gt-fasthtml.png)