Skip to content

Instantly share code, notes, and snippets.

View ktmud's full-sized avatar
🥌
Peace and Love~

Jesse Yang ktmud

🥌
Peace and Love~
View GitHub Profile
@ktmud
ktmud / useAsyncState.ts
Created July 12, 2024 15:37
Useful hooks
import { useEffect, useState } from 'react';
/**
* Load data from an async function and update a state.
*/
export default function useAsyncState<TValue>(loader: Promise<TValue> | (() => Promise<TValue>)) {
const [value, setValue] = useState<TValue>();
useEffect(() => {
const promise = typeof loader === 'function' ? loader() : loader;
promise.then(setValue);
@ktmud
ktmud / FlashingDots.tsx
Created September 8, 2022 05:13
Chakra UI FlashingDots
import {
chakra,
keyframes,
CSSObject,
ChakraProps,
useToken,
} from '@chakra-ui/react';
export type FlashingDotsProps = Pick<ChakraProps, 'width' | 'height'> & {
/**
@ktmud
ktmud / app.py
Created May 20, 2022 21:58
aiohttp server with file system based router
import pkgutil
from aiohttp import web
ALLOWED_METHODS = ["GET", "POST"]
def load_route_handlers(path, mod):
routes = []
for method in ALLOWED_METHODS:
if hasattr(mod, method):
@ktmud
ktmud / __init__.py
Last active May 24, 2024 07:05
An extreme simple Python http server with auto reload and file-system based router
import os
from .app import start_cli_service
env = (os.environ.get("env") or "prod").lower()
is_dev = env == "dev" or env == "local"
port, autoreload_observer = start_cli_service(autoreload=is_dev)
if autoreload_observer:
# move autoreload observer to the foreground so process won't exit
@ktmud
ktmud / sqlalchemy_insert_from_select.py
Last active March 29, 2022 22:15
Understand how SQLAlchemy insert().from_select(..) works
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Model(Base):
__tablename__ = "model"
id = sa.Column("id", primary_key=True)
bbb = sa.Column("bbb")
@ktmud
ktmud / FlashingDots.tsx
Created September 13, 2021 22:49
CSS Loading Indicator Three Flashing Dots in styled-jsx
export type FlashingDotsProps = {
/**
* Size of each dot
*/
dotSize?: number;
/**
* Animation duration.
*/
duration?: number;
/**
@ktmud
ktmud / remove_duplicate.py
Created March 14, 2021 06:57
Python Remove Duplicates by Key
def remove_duplicates(
items: Iterable[InputType], key: Optional[Callable[[InputType], Any]] = None
) -> List[InputType]:
"""Remove duplicate items in an iterable."""
if not key:
return list(dict.fromkeys(items).keys())
seen = set()
result = []
for item in items:
item_key = key(item)
@ktmud
ktmud / cancel_github_workflows.py
Last active January 9, 2023 19:59
Manually cancel previous GitHub Action workflow runs in queue
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
import {
ReactNode,
HTMLAttributes,
CSSProperties,
HTMLProps,
MutableRefObject,
FunctionComponent,
} from 'react';
declare module 'react-syntax-highlighter' {
@ktmud
ktmud / superset_exception.py
Last active July 14, 2020 16:38
API Design in Python/Flask - Generic Exceptions with Error Codes
from enum import Enum, IntEnum
from typing import Any, Dict, Optional
from flask import jsonify
from flask_babel import gettext as _, ngettext
from superset.typing import FlaskResponse
class SupersetErrorCode(IntEnum):
# Generic Superset errors