Bibliothèque d’extraits

Des extraits testés, prêts à copier, en Python, Flask, JavaScript, SQL, HTML, CSS et Bash. Ouvrez-les dans le playground.

46 extraits

html

Accessible modal with <dialog>

A native modal with focus handling, Escape to close and a backdrop.

beginner #accessibility#dialog#ui
flask

Application factory

Create the app in a function so tests and production can each build their own.

beginner #structure#config#blueprints
javascript

Debounce

Run a function only after the user stops typing or resizing.

beginner #performance#events#functions
javascript

Fetch JSON with a timeout

Abort a fetch that takes too long and handle HTTP errors.

intermediate #fetch#network#async
python

Read a JSON file with clear errors

Load a JSON file and get a readable message when it is missing or malformed.

beginner #json#files#errors
css

Responsive card grid without media queries

Cards wrap to as many columns as fit, with a sensible minimum width.

beginner #grid#layout#responsive
python

Retry a flaky call with exponential backoff

A decorator that retries a function on chosen exceptions, waiting longer after each failure.

intermediate #retry#decorators#network
flask

Safe file upload

Check the extension, size and filename before saving an upload.

intermediate #uploads#security#files
bash

Safe script header

Stop on errors, unset variables and failed pipes, and clean up on exit.

beginner #scripting#errors
sql

Upsert: insert or update

Insert a row, or update it when the unique key already exists.

intermediate #postgres#upsert#writes
bash

Back up a PostgreSQL database

Dump, compress, and keep only the last 14 backups.

intermediate #postgres#backups#cron
python

Cache expensive results

Memoize a pure function with functools.lru_cache.

beginner #performance#functools#recursion
html

Contact form with built-in validation

Labels, input types and constraints that browsers validate for free.

beginner #forms#validation#accessibility
javascript

Copy text to the clipboard

Use the async clipboard API with a fallback for older browsers.

beginner #clipboard#dom#ux
css

Dark and light theme with variables

Define colours once, switch them with a data attribute.

intermediate #theming#dark-mode#variables
javascript

Deep clone an object

structuredClone copies nested objects, Dates, Maps and Sets correctly.

beginner #objects#data
javascript

Event delegation

One listener on a parent handles clicks for current and future children.

intermediate #dom#events#performance
sql

Find duplicate rows

List values that appear more than once, with their count.

beginner #data-quality#group-by