https://github.com/answerdotai/fh-pyinstrument
Using pyinstrument to profile FastHTML apps
https://github.com/answerdotai/fh-pyinstrument
Last synced: 7 months ago
JSON representation
Using pyinstrument to profile FastHTML apps
- Host: GitHub
- URL: https://github.com/answerdotai/fh-pyinstrument
- Owner: AnswerDotAI
- License: apache-2.0
- Created: 2025-03-22T02:14:40.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-03-27T01:36:00.000Z (11 months ago)
- Last Synced: 2025-07-18T21:21:20.557Z (7 months ago)
- Language: Jupyter Notebook
- Homepage: https://answerdotai.github.io/fh-pyinstrument/
- Size: 396 KB
- Stars: 1
- Watchers: 7
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fh-pyinstrument
Sometimes when building FastHTML apps we run into performance
bottlenecks. Figuring out what is slow can be challenging, especially
when building apps with async components. That’s where profiling tools
like [pyinstrument](https://pyinstrument.readthedocs.io/en/latest/) can
help. Profilers are tools that show exactly how long each component of a
project takes to run. Identifying slow parts of an app is the first step
in figuring out how to make things run faster.

## How to install
Install from PyPI using uv:
``` sh
uv pip install fh-pyinstrument
```
Or with classic pip:
``` sh
pip install fh-pyinstrument
```
## How to configure
The easiest way is to import the ProfileMiddleware into your project and
add it to your app’s list of Middleware via the `app.add_middleware()`
method:
``` python
from fasthtml.common import *
from fh_pyinstrument.core import ProfileMiddleware
app, rt = fast_app()
app.add_middleware(ProfileMiddleware)
@rt
def index(): return Titled('Hello, profiler!')
serve()
```
If you want to add it to the project when `fast_app()` is declared,
you’ll need to run it through Starlette’s middleware pre-processor:
``` python
from fasthtml.common import *
from fh_pyinstrument.core import ProfileMiddleware
from starlette.middleware import Middleware
app, rt = fast_app(middleware=(Middleware(ProfileMiddleware)))
@rt
def index(): return Titled('Hello, profiler!')
serve()
```
## How to use the middleware
Simply add `?profile=1` to any url, that will cause the app to display
an amazing chart set in the browser. In the example above, run it and
click this link:
If instead you want to have the results show up in the terminal, also
add `term=1` to the query string. The normal web page will display in
your browser, and the pyinstrument view will show up in limited form
within the terminal.
## How to use the stand-alone `@instrument` decorator
If you want to temporarily use fh-pyinstrument on an isolated route
handler, the `@instrument` decorator can be used:
``` python
from fh_pyinstrument.core import instrument
@rt
@instrument
def index(): return Titled('Hello, profiler!')
```
## Developer Guide
If you are new to using `nbdev` here are some useful pointers to get you
started.
### Install fh-pyinstrument in Development mode
Clone locally:
``` sh
gh repo clone answerdotai/fh-pyinstrument
```
Then install:
``` sh
# make sure fh-pyinstrument package is installed in development mode
$ pip install -e .
# make changes under nbs/ directory
# ...
# compile to have changes apply to fh-pyinstrument
$ nbdev_prepare
```