Skip to content

Instantly share code, notes, and snippets.

View neilmcguigan's full-sized avatar

Neil McGuigan neilmcguigan

  • Vancouver, Canada
  • 11:38 (UTC -08:00)
View GitHub Profile
View Jinja.jinja
1. Jinja Macros can return results (but only if `called`):
```
{% macro foo() %}
hello from foo
{{ caller({"bar":1}) if caller is defined }}
{% endmacro %}
{% call(result) foo() %}
{{result.bar}}
@neilmcguigan
neilmcguigan / optimistic-locking.py
Created April 1, 2023 23:16
A sample app to show how to use optimistic concurrency / optimistic locking in Flask and SQLAlchemy
View optimistic-locking.py
"""
A sample app to show how to use optimistic concurrency / optimistic locking in Flask and SQLAlchemy
The trick is to store your entity in session between requests
You need to use Flask-Session, so can serialize your entities properly
Usage:
flask --app optimistic-locking.py run
navigate to http://127.0.0.1:5000/edit/1
@neilmcguigan
neilmcguigan / fastapi-htmx
Last active March 8, 2023 23:03
FastAPI HTMX
View fastapi-htmx
# main.py
@app.middleware("http")
async def set_xhr(request: Request, call_next):
request.state.is_xhr = "HX-Request" in request.headers
return await call_next(request)
# in your Jinja2 template :
{% if request.state.is_xhr %}
View create-databasechangelog.sql
# TAG is a Redshift keyword, so we have to create this table ourselves, and quote TAG
# otherwise, you need a custom database class for liquibase
# see https://github.com/liquibase/liquibase-redshift
CREATE TABLE public.databasechangelog (
ID VARCHAR(255) NOT NULL,
AUTHOR VARCHAR(255) NOT NULL,
FILENAME VARCHAR(255) NOT NULL,
DATEEXECUTED TIMESTAMP WITHOUT TIME ZONE NOT NULL,
ORDEREXECUTED INTEGER NOT NULL,