Skip to content

Instantly share code, notes, and snippets.

View ahopkins's full-sized avatar

Adam Hopkins ahopkins

View GitHub Profile

Sanic Live Reload

Uses livereload.js to run a live reload server with Sanic. Startup, with auto-reload enabled. In this example, we also are adding an extra directory to listen to:

sanic server:app --dev -R ./content

Run the example. Open http://127.0.0.1:8000/ in a browser with a livereload extension installed. Make a change to ./content/hello.txt and save. Your browser should now update to the new text.

from __future__ import annotations
import re
from dataclasses import dataclass, field
from typing import Iterator, List, Optional
from urllib.parse import urlencode
from rich import print
@ahopkins
ahopkins / flexibly_decorator.py
Last active January 3, 2023 10:00
Decorator to cover all use cases
from abc import ABC, abstractmethod
from functools import partial, wraps
class BaseFlexibleDecorator(ABC):
__name__: str
def __init__(self, func=None, *decorator_args, **decorator_kwargs):
if func:
wraps(func)(self)
@ahopkins
ahopkins / # Sanic websocket feeds v3.md
Last active April 23, 2024 15:21
Sanic websocket feeds - v3

Sanic Websockets Feeds v3

This is an example of how to build a distributed websocket feed. It allows for horizontal scaling using Redis as a pubsub broker to broadcast messages between application instances.

This is the third version of websocket feeds. It is built with Sanic v21.9+ in mind. Older versions:

@ahopkins
ahopkins / # Sanic as Svelte development server.md
Last active June 2, 2022 23:04
Sanic as Svelte development server

Sanic as Svelte development server

The purpose of this gist is to show how Sanic can be used as a development server for a frontend JS framework. In this example we are running a Svelte app with rollup, but the same idea could be applied to any other frameworks JS build tools.

Run with:

sanic path.to:app -d -R ./path/to/public
@ahopkins
ahopkins / # Server Sent Events.md
Last active January 20, 2022 12:45
Server Sent Events

Server Sent Events

@ahopkins
ahopkins / # Sanic Benchmarking.md
Last active November 15, 2021 12:10
Sanic Benchmarking

Sanic Benchmarking

@ahopkins
ahopkins / # Experiment for AST router.md
Last active November 15, 2021 12:10
Experiment for AST router

Experiment for AST router

@ahopkins
ahopkins / components.selected-input.js
Created October 2, 2019 22:25
moment-selected-input
import TextField from '@ember/component/text-field'
export default TextField.extend({
focusIn: function (event) {
this.$().select()
}
});
@ahopkins
ahopkins / custom_authentication_cls_complex.py
Created May 13, 2019 08:00
custom_authentication_cls_complex.py
import random
from datetime import datetime
from jwt.exceptions import DecodeError
from sanic import Sanic
from sanic.response import json
from sanic_jwt import (
Authentication,
Claim,
Configuration,