Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/jrycw/gt-fasthtml
- Owner: jrycw
- License: mit
- Created: 2024-08-17T04:33:03.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-10-05T12:50:58.000Z (5 months ago)
- Last Synced: 2024-12-04T12:37:39.943Z (3 months ago)
- Topics: fasthtml, pandas, polars, python, table
- Language: Python
- Homepage:
- Size: 116 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).
