Snippet library

Copy-ready, tested snippets for Python, Flask, JavaScript, SQL, HTML, CSS and Bash. Open any of them in the playground.

46 snippets

sql

Find slow queries

The queries using the most total time (needs the pg_stat_statements extension).

advanced #postgres#performance#monitoring
bash

Find the biggest files and folders

See what is filling the disk.

beginner #linux#disk#admin
javascript

Format money and dates for any locale

Let Intl format currency, numbers and dates instead of string hacks.

beginner #intl#i18n#formatting
python

Generate a secure random token

Use the secrets module (never random) for passwords, tokens and reset links.

beginner #security#secrets#auth
javascript

Group an array by a key

Build an object of arrays with reduce (works everywhere, unlike Object.groupBy).

beginner #arrays#data#objects
python

Group items by a key

Turn a flat list into a dict of lists with collections.defaultdict.

beginner #collections#dict#data
flask

JSON error handlers

Return JSON instead of HTML for API errors.

beginner #api#errors#json
sql

Keyset pagination

Page through big tables without the slowdown of large OFFSET values.

intermediate #pagination#performance
flask

Login-required decorator for JSON routes

Answer 401 JSON instead of redirecting to the login page.

intermediate #auth#decorators#api
flask

Paginate a query

Paginate with Flask-SQLAlchemy and keep page numbers safe.

beginner #sqlalchemy#pagination#database
flask

Rate limit a route

Stop brute-force attempts with Flask-Limiter.

beginner #security#rate-limit#login
python

Read and write CSV files

Read rows as dicts and write a filtered copy with csv.DictWriter.

beginner #csv#files#data
css

Respect reduced motion

Turn animations off for people who asked their system for less motion.

beginner #accessibility#animation
html

Responsive image

Serve the right size and format, and avoid layout shift.

beginner #images#performance
sql

Running total

Cumulative revenue per day with SUM() OVER.

intermediate #window-functions#reports
javascript

Run promises with a concurrency limit

Process many async jobs, but only N at a time.

advanced #async#promises#performance
bash

Search code fast

Find where something is used, ignoring virtualenvs and builds.

beginner #grep#search#git
flask

Send CSRF token with fetch

Put the token in a meta tag and send it as X-CSRFToken from JavaScript.

beginner #csrf#security#javascript