Light Mode

Simon Willison's Weblog

Subscribe

Sunday, 1st September 2024

Release datasette-acl 0.3a0 -- Advanced permission management for Datasette
1st Sep 2024, 4:12 am * datasette

uvtrick (via) This "fun party trick" by Vincent D. Warmerdam is absolutely brilliant and a little horrifying. The following code:

from uvtrick import Env

def uses_rich():
from rich import print
print("hi :vampire:")

Env("rich", python="3.12").run(uses_rich)

Executes that uses_rich() function in a fresh virtual environment managed by uv, running the specified Python version (3.12) and ensuring the rich package is available - even if it's not installed in the current environment.

It's taking advantage of the fact that uv is so fast that the overhead of getting this to work is low enough for it to be worth at least playing with the idea.

The real magic is in how uvtrick works. It's only 127 lines of code with some truly devious trickery going on.

That Env.run() method:

  • Creates a temporary directory
  • Pickles the args and kwargs and saves them to pickled_inputs.pickle
  • Uses inspect.getsource() to retrieve the source code of the function passed to run()
  • Writes that to a pytemp.py file, along with a generated if __name__ == "__main__": block that calls the function with the pickled inputs and saves its output to another pickle file called tmp.pickle

Having created the temporary Python file it executes the program using a command something like this:

uv run --with rich --python 3.12 --quiet pytemp.py

It reads the output from tmp.pickle and returns it to the caller!

# 5:03 am / python, uv, vincent-d-warmerdam, rich

Release datasette-acl 0.4a0 -- Advanced permission management for Datasette
1st Sep 2024, 7:47 pm * datasette
Release datasette-acl 0.4a1 -- Advanced permission management for Datasette
1st Sep 2024, 9:39 pm * datasette
Release datasette-acl 0.4a2 -- Advanced permission management for Datasette
1st Sep 2024, 9:50 pm * datasette
Release datasette-pins 0.1a4 -- Pin databases, tables, and other items to the Datasette homepage
1st Sep 2024, 10:15 pm * datasette
- Saturday, 31st August 2024
Monday, 2nd September 2024 -

2024 >> September

MTWTFSS
1
2345678
9101112131415
16171819202122
23242526272829
30