Skip to content

Instantly share code, notes, and snippets.

View rmorshea's full-sized avatar

Ryan Morshead rmorshea

View GitHub Profile
@rmorshea
rmorshea / poor_mans_use_state.py
Last active July 10, 2023 19:40
Poor Man's Use State Hook
from contextlib import contextmanager
def use_state(value):
hook = current_hook()
state = hook.use_state(lambda: {"value": value})
return state["value"], lambda new_value: state.__setitem__("value", new_value)
def current_hook():
@rmorshea
rmorshea / reactpy.html
Created July 9, 2023 22:15
ReactPy + PyScript
<html>
<head>
<title>ReactPy</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<div id="root"></div>
<py-config>
@rmorshea
rmorshea / reactpy-v2.md
Last active July 10, 2023 08:22
ReactPy in the Browser

A Vision for ReactPy 2.0 (maybe 3.0?)

Currently, ReactPy runs entirely server-side - all views are rendered, and their state is maintained on the server. This is how most other applications that have given Python the ability to drive interactivity have been built. Examples can be found in:

  • IpyWidgets
  • Solara
  • Streamlit
  • Pynecone

This design has opened up a whole world of possibilities for applications written purely with Python, however it has a significant drawback. In particular, this approach of requiring the server to retain a client's state and render views means that you are fundamentally limited in how many users you can serve. In many cases this has meant that these tools tend to be used to build interal data apps that serve a small number of users. Traditionally built applications avoid this problem by delegating much of this work to the client, only reaching out to a server when absolutely necessary. Even though tools like PyScript and Pyodide now allow Python to run client-side, with that

@rmorshea
rmorshea / nox_groups.py
Created February 13, 2023 03:03
A way to define groups of sessions with a common set of cli parameters.
from __future__ import annotations
import argparse
import inspect
import functools
from typing import Any, Callable, TypeVar, get_type_hints
import nox
from nox.sessions import Session
from idom import html
# <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
# <div class="modal-dialog">
# <div class="modal-content">
# <div class="modal-header">
# <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
# <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
# </div>
import sys
from inspect import signature, Signature, Parameter, BoundArguments, cleandoc
from argparse import ArgumentParser, RawTextHelpFormatter
from typing import TypeVar, Callable
F = TypeVar("F", bound=Callable[..., None])
class Commands:
@rmorshea
rmorshea / auto-activate-venv.sh
Created July 15, 2022 23:12
Auto activate venv in current directory
#!/bin/bash
# activate Python venv in present dir
function cd() {
if [[ -d ./.venv ]] && [[ $(which python) = $PWD* ]]; then
deactivate
fi
builtin cd $1
if [[ -d ./.venv ]]; then
@rmorshea
rmorshea / snake_game_class.py
Last active October 22, 2021 02:29
Class-based Snake Game With IDOM
import asyncio
import enum
import random
import time
from dataclasses import dataclass, replace
import idom
class GameState(enum.Enum):
@rmorshea
rmorshea / idom_snake.py
Last active March 27, 2019 15:01
The game Snake implemented in Python with iDOM (https://github.com/rmorshea/idom)
import idom
import enum
import time
import random
import asyncio
class WASD(enum.Enum):
w = (-1, 0)
@rmorshea
rmorshea / idom_snake.py
Created February 28, 2019 22:47
The game Snake implemented in Python with iDOM (https://github.com/rmorshea/idom)
import idom
import enum
import time
import random
import asyncio
class WASD(enum.Enum):
w = (-1, 0)