Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@simonw
simonw / catjq
Created June 22, 2019 19:26
I have this in ~/bin/catjq - chmod 755 - from https://news.ycombinator.com/item?id=20246957
#!/usr/local/bin/jq -rf
tostream
| select(length > 1)
| (
.[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | join("")
@simonw
simonw / starlette_demo.py
Created July 14, 2019 01:37
datasette_auth_github middleware example, from https://simonwillison.net/2019/Jul/14/sso-asgi/
from datasette_auth_github import GitHubAuth
from starlette.applications import Starlette
from starlette.responses import HTMLResponse
import uvicorn
app = Starlette(debug=True)
@app.route("/")
async def homepage(request):
@simonw
simonw / log_to_sqlite.py
Created September 30, 2019 16:22
Class for logging structlog entries to a SQLite database
# Use it like this:
#
# structlog.configure(
# processors=[
# structlog.processors.JSONRenderer(),
# LogToSqlite("/tmp/logs.db")
# ]
# )
# log = structlog.get_logger()
# log.msg("say-hello", whom="world", num=[random.randint(1,55)])
@simonw
simonw / pool.py
Created October 5, 2019 17:46
Earlier attempt at Datasette connection pooling, Version from 8am 2nd October 2019
import contextlib
import threading
import sqlite3
class Connection:
def __init__(self, name, db_conn):
self.name = name
self.db_conn = db_conn
self.available = True
{
"install": "pip3 install --user -r requirements.txt",
"start": "PYTHONUNBUFFERED=true python3 server.py",
"watch": {
"ignore": [
"\\.pyc$"
],
"install": {
"include": [
"^requirements\\.txt$"
@simonw
simonw / deepest.sql
Created October 6, 2019 17:15
Query a twitter-to-sqlite database and find the "deepest" tweets in a reply thread, adapted from https://gist.github.com/robinhouston/f689a4b833dc027a3fd97e3de855927b
with recursive thread as (
select id, in_reply_to_status_id, 0 as depth
from tweets
where in_reply_to_status_id is null
union
select tweets.id, tweets.in_reply_to_status_id, 1 + thread.depth as depth
from thread join tweets on tweets.in_reply_to_status_id = thread.id)
select * from thread order by depth desc
@simonw
simonw / autoescape.js
Created October 21, 2019 18:02
An autoescaping JavaScript template tagged literal
class Safe extends String {}
function safe(s) {
if (!(s instanceof Safe)) {
return new Safe(s);
} else {
return s;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
async def data(
self,
request,
database, # Name of the database
hash, # Hash component of the database URL (usually ignored)
table, # Name of the table
default_labels=False, # HTML views default to expanding all foreign key labels
_next=None,
_size=None,
{
"title": "This is the title"
}