Skip to content

Instantly share code, notes, and snippets.

Simon Willison simonw

Block or report user

Report or block simonw

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Big Local Louisiana.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@simonw
simonw / autoescape.js
Created Oct 21, 2019
An autoescaping JavaScript template tagged literal
View autoescape.js
class Safe extends String {}
function safe(s) {
if (!(s instanceof Safe)) {
return new Safe(s);
} else {
return s;
}
}
@simonw
simonw / deepest.sql
Created Oct 6, 2019
Query a twitter-to-sqlite database and find the "deepest" tweets in a reply thread, adapted from https://gist.github.com/robinhouston/f689a4b833dc027a3fd97e3de855927b
View deepest.sql
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
View glitch.json
{
"install": "pip3 install --user -r requirements.txt",
"start": "PYTHONUNBUFFERED=true python3 server.py",
"watch": {
"ignore": [
"\\.pyc$"
],
"install": {
"include": [
"^requirements\\.txt$"
@simonw
simonw / pool.py
Created Oct 5, 2019
Earlier attempt at Datasette connection pooling, Version from 8am 2nd October 2019
View pool.py
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
@simonw
simonw / log_to_sqlite.py
Created Sep 30, 2019
Class for logging structlog entries to a SQLite database
View log_to_sqlite.py
# 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)])
View starlette_demo.py
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 / catjq
Created Jun 22, 2019
I have this in ~/bin/catjq - chmod 755 - from https://news.ycombinator.com/item?id=20246957
View catjq
#!/usr/local/bin/jq -rf
tostream
| select(length > 1)
| (
.[0] | map(
if type == "number"
then "[" + tostring + "]"
else "." + .
end
) | join("")
@simonw
simonw / installing-mysqldb.md
Created Jun 21, 2019
Installing MySQLdb mysqlclient with Homebrew and pip in OS X Mojave
View installing-mysqldb.md

Installing MySQLdb mysqlclient with Homebrew and pip in OS X Mojave

First I used Homebrew to install the MySQL server along with mysql_config, needed to compile the Python extension:

brew install mysql

I had to then use this incantation to get pip install mysqlclient to work:

LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/ pip install mysqlclient
@simonw
simonw / example-Locations.xml
Last active Jul 21, 2019
Convert Locations.kml (pulled from an iPhone backup) to SQLite
View example-Locations.xml
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<TimeStamp>
<when>2015-12-18T19:12:32</when>
</TimeStamp>
<name>2015-12-18 19:12:32 Source: WhatsApp</name>
<Point>
<coordinates>-0.120970480144024,51.510383605957</coordinates>
You can’t perform that action at this time.