Document how to provide a negative number as an argument · fastapi typer · Discussion #798
First Check I added a very descriptive title here. I used the GitHub search to find a similar question and didn't find it. I searched the Typer documentation, with the integrated search. I already ...
GitHub · github.com [1]
Today I learned that you cannot pass negative integers as values to typer. in this case context_settings={"ignore_unknown_options": True} is required so that the - does not look like a flag.
# script name: main.py
import typer
app = typer.Typer()
@app.command()
def failing(value: float):
print(f"{value=}")
@app.command(
context_settings={"ignore_unknown_options": True}
)
def working_good(value: float):
print(f"{value=}")
if __name__ == "__main__":
app()
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://github.com/fastapi/typer/discussions/798
[2]: /thoughts/
Posts tagged: python
All posts with the tag "python"
312 posts
latest post 2026-05-06
Publishing rhythm
External Link
X (formerly Twitter) · x.com [1]
This new demo of textual is wildly awesome, so many widgets and features being added into the main library. The themes and animations are on point and far surpass my expectations of a tui.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://x.com/willmcgugan/status/1849831271289012463
[2]: /thoughts/
Pin versions of dependencies · Issue #2200 · Kozea/WeasyPrint
Stack trace: pdf = HTML(file_obj=html, base_url='/').write_pdf() File "/service/venv/lib/python3.9/site-packages/weasyprint/__init__.py", line 252, in write_pdf self.render(font_config, counter_sty...
GitHub · github.com [1]
weazyprint was throwing me some errors, turns out that it’s currently not compatible with the latest pydyf package.
my error
TypeError: __init__() takes 1 positional argument but 3 were give
I fixed it by locking in pydyf at 0.8.0
pydyf==0.8.0
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://github.com/Kozea/WeasyPrint/issues/2200
[2]: /thoughts/
Installing and managing Python | uv
A guide to using uv to install Python, including requesting specific versions, automatic installation, viewing installed versions, and more.
docs.astral.sh [1]
uv now can install python for you. uv is inspired by rust’s cargo, an all in one fast package and installation manager. uv is so fast and becoming feature complete, it’s really changing the python packaging ecosystem.
uv python install
uv python install 3.12
uv python list
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://docs.astral.sh/uv/guides/install-python/
[2]: /thoughts/
-
Hard to argue this take, happy to see that its at the top. With it being such an old language its amazing that it still holds this position, and not surprising that it has warts, and thing that have turn users off from wanting anything to do with it.
timestamped in the link
Note
This post is a thought [1]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: /thoughts/
FastHX - FastHX
volfpeter.github.io [1]
Very interesting approach to htmx [2] and fast api. It uses separate decorators for returning template partials and json that can be stacked to include both options on a single route. The templates are explicitly set in the decorator. Separate decorators are used for full page and partial pages. I don’t see an example of full and partial pages being combined. I think the demo app must be behaving in a spa like fashion where it does not get all of the data when it calls index and index will ask for user-list.
Definitely going to keep my eye on this project and ponder on it.
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates
from fasthx import Jinja
from pydantic import BaseModel
# Pydantic model of the data the example API is using.
class User(BaseModel):
first_name: str
last_name: str
# Create the app.
app = FastAPI()
# Create a FastAPI Jinja2Templates instance and use it to create a
# FastHX Jinja instance that will serve as your decorator.
jinja = Jinja(Jinja2Templates("templates"))
@app.get("/")
@jinja.page("index.html")
def index() -> None:
...
@app.get("/user-list")
@jinja.hx("user-list.html")
async...
FastHX - FastHX
volfpeter.github.io [1]
Very interesting approach to htmx [2] and fast api. It uses separate decorators for returning template partials and json that can be stacked to include both options on a single route. The templates are explicitly set in the decorator. Separate decorators are used for full page and partial pages. I don’t see an example of full and partial pages being combined. I think the demo app must be behaving in a spa like fashion where it does not get all of the data when it calls index and index will ask for user-list.
Definitely going to keep my eye on this project and ponder on it.
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates
from fasthx import Jinja
from pydantic import BaseModel
# Pydantic model of the data the example API is using.
class User(BaseModel):
first_name: str
last_name: str
# Create the app.
app = FastAPI()
# Create a FastAPI Jinja2Templates instance and use it to create a
# FastHX Jinja instance that will serve as your decorator.
jinja = Jinja(Jinja2Templates("templates"))
@app.get("/")
@jinja.page("index.html")
def index() -> None:
...
@app.get("/user-list")
@jinja.hx("user-list.html")
async...
Background Tasks - FastAPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production
fastapi.tiangolo.com [1]
fastapi [2] comes with a concept of background tasks which are functions that can be ran in the background after a function has been ran. This is handy for longer running functions that may take some time and you want to have fast response times.
Here is an example from the docs
from fastapi import BackgroundTasks, FastAPI
app = FastAPI()
def write_notification(email: str, message=""):
with open("log.txt", mode="w") as email_file:
content = f"notification for {email}: {message}"
email_file.write(content)
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
background_tasks.add_task(write_notification, email, message="some notification")
return {"message": "Notification sent in the background"}
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://fastapi.tiangolo.com/tutorial/background-tasks/
[2]: /fastapi/
[3]: /thoughts/
markdown-it-pyrs
A Python interface for markdown-it.rs, using Rust for blazingly fast Markdown parsing ⚡️
PyPI · pypi.org [1]
markdown it py running in rust claims to be 20x faster. I’ll definitely look into this if markdown it py is ever a bottleneck in my performance. At first glance it appears that plugins are written in rust not python, and there is no admonition plugin, so I’ll keep my eye on it for now, but I can’t use it.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://pypi.org/project/markdown-it-pyrs/
[2]: /thoughts/
[1]
diskcache has a peekitem method that allows you to lookup the expire_time of a cached item without changing it. I recently used this to implement debounce for fastapi [2] background tasks with multiple workers running. since all the workers I care about are on the same machine, but running in different processes diskcache was a great option. All workers have access to the same disk, but not the same variables in memory.
Note
This post is a thought [3]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: /static/https://grantjenks.com/docs/diskcache/api.html#diskcache.Cache.peekitem
[2]: /fastapi/
[3]: /thoughts/
I’ve been using fastapi [1] more and more lately and one feature I just started
using is background tasks [[ thoughts-333 ]].
Seealso
basic diskcache example <a href="/python-diskcache/" class="wikilink" data-title="How I setup a sqlite cache in python" data-description="When I need to cache some data between runs or share a cache accross multiple processes my go to library in python is . It's built on sqlite with just enough..." data-date="2022-03-29">How I setup a sqlite cache in python</a>
One Background Task per db entry # [2]
I am using it for longer running tasks and I don’t want to give users the
ability to spam these long running tasks with many duplicates running at the
same time. And each fastapi worker will be running in a different process so I
cannot keep track of work in memory, I have to do it in a distributed fashion.
Since they are all running on the same machine with access to the same disk,
diskcache is a good choice
What I need # [3]
- check if a job is running
- automatically expire jobs
Less infrastructure complexity # [4]
My brain first went to thinking I needed another service like redis running
alongside fastapi for this, then it hit me that...
GitHub - dropbox/pyannotate: Auto-generate PEP-484 annotations
Auto-generate PEP-484 annotations. Contribute to dropbox/pyannotate development by creating an account on GitHub.
GitHub · github.com [1]
pyannotate is a tool that uses runtime types to suggest type annotations to use. Test runners such as pytest can be used to generate the types.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://github.com/dropbox/pyannotate
[2]: /thoughts/
GitHub - tusharsadhwani/yen: The last Python environment manager you'll ever need.
The last Python environment manager you'll ever need. - tusharsadhwani/yen
GitHub · github.com [1]
Create virtual environments for any Python version, without needing Python installed.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://github.com/tusharsadhwani/yen
[2]: /thoughts/
Best practices for Docker in production
You
talkpython.fm [1]
Great listen for anyone interested in productionizing python code with docker. Itamar brings up some
Don’t trust base images for security, upgrade your packages. Vulnerabilties become published and solved giving the bad guys istructions how to wreck your day and these fixes wont come to your docker application for up to two weeks due to image build tatency.
For job based containers pre-compile your pyc for faster startup.
Alpine linux is probably not what you want for python. Many packages such as postgres ship pre-copiled binaries that work for most linux distributions wich use glibc, but alpine uses musl so the binaries will be incompatable requiring you to need to install a bunch of build dependencies.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://talkpython.fm/episodes/show/323/best-practices-for-docker-in-production
[2]: /thoughts/
xxHash - Extremely fast non-cryptographic hash algorithm
xxhash.com [1]
xxHash is an extremely fast non-cryptographic hash algorithm, working at RAM speed limit. It is proposed in four flavors (XXH32, XXH64, XXH3_64bits and XXH3_128bits). The latest variant, XXH3, offers improved performance across the board, especially on small data.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://xxhash.com/
[2]: /thoughts/
xxhash
Python binding for xxHash
PyPI · pypi.org [1]
I hit an issue with markata where even though a bunch of articles were cached, the site build was still slow because I was hitting hashlib.sha256 so hard for cache keys. I was shocked when this popped up in my profiler as a significant portion of the time spent. I swapped out for xxhash and that issue completely went away.
Note
This post is a thought [2]. It’s a short note that I make
about someone else’s content online #thoughts
References:
[1]: https://pypi.org/project/xxhash/
[2]: /thoughts/
I just implemented a latest blog post link in Markata by asking for the first
post slug from the blog feed. The implementation uses the jinja_md plugin to
render jinja against the markdown and a tag to redirect.
My latest blog post is [[ {{ markata.feeds.blog.posts[0].slug }} ]]. Click the
link if you are not automatically redirected.
<meta http-equiv="Refresh" content="0; url='/{{ markata.feeds.blog.posts[0].slug }}'" />
Setting up the feed # [1]
Feeds are setup in markata.toml configuration. They provide a handy way to
create an html [2] feed, rss feed, and quickly reference a filtered set of posts
like this.
# you will need to enable the jinja_md plugin along with the defaults
[markata]
hooks = [
"markata.plugins.jinja_md",
"default",
]
# set up the blog feed
[[markata.feeds]]
slug = 'blog'
template = "feed.html"
filter = "date<=today and templateKey in ['blog-post'] and published"
sort = "date"
reverse = true
For more information on markata check out the full markata [3] post.
References:
[1]: #setting-up-the-feed
[2]: /html/
[3]: /markata/
sick wikilink hover
Today I set up some sick wikilink hover effects using tailwind see A Case For Tailwindcss [1]. When you hover over them they show an image preview
of the link that you are going to. I cant find where I have seen this but it
comes from some docs sites.
I’ll finish this article later, just excited to see it up.
References:
[1]: /a-case-for-tailwindcss/
markata
This post is a work in progress.
Markata is the static site generator that I created to build my website about this site [1]. I built it for me and I enjoy using it. I know everying
it can do and I can extend it to do more easily. I have set it up for some
friends to also use it and am proud that it helps them publish their content.
It’s a meme to create your own static site generator to make your website. Yes
its funny, I don’t recommend it if your not ready for the level of work that
comes with it, but at the end of the day it’s very rewarding and a great way to
learn.
Static Sites were all the rage # [2]
JAMStack was 🔥
Gatsby and Next.js hit the scene as the next generation of static site builders
and were getting big around the time I started building my site in 2017. They
were based on react. I dove into react and learned it enough to build my
website, but I really lacked the depth of knowledge in the js ecosystem to
really work on it effectively. For instance when it got ...
thinking about static sites in 2024
actions build
k8s build
fastapi [1] sqlite
References:
[1]: /fastapi/