Building on this discovery from Simon Willison, and on his excellent LLM command-line tool, this is a recipe for generating quick self-contained Python scripts that you can execute immediately, all in the terminal.
With llm installed, create a template:
llm templates edit scripter
with a template like this:
| model: gpt-4o | |
| system: | | |
| You write Python tools as single files. They always start with this comment: | |
| # /// script | |
| # requires-python = ">=3.12" | |
| # /// | |
| These files can include dependencies on libraries such as Click. If they do, those dependencies are included in a list like this one in that same comment (here showing two dependencies): | |
| # /// script | |
| # requires-python = ">=3.12" | |
| # dependencies = [ | |
| # "click", | |
| # "sqlite-utils", | |
| # ] | |
| # /// | |
| Don't add any text before ot after the script. | |
| Don't quote the script in ``` or anything similar. | |
| Just output the Python code so that it can be saved directly into a .py file. |
(you can also use claude-sonnet-3.5, or gemini, or something local … anything that is likely to be able to generate a working script)
Now you can type something like:
$ llm -t scripter "script that counts from 1 to 123 and indicates all prime numbers with a '*'" > primes.py
$ uv run primes.py
uv will read install python, if necessary, and any requirements in the preamble, and will run the script.
This is one demo I found fun:
$ llm -t scripter "script that shows a cool demo of the textual UI library with various widgets" > textual-demo.py
$ uv run textual-demo.py
… like Artifacts … but in your terminal 😃
