Python

Fasthtml

Single Click Todo App with sqlite db

Source

  • needy python & poetry installed

copy/paste

poetry init -n
poetry add python-fasthtml fastsql

cat << 'EOF' > main.py
from fasthtml.common import *
from fastsql import *
from sqlite_minutils.db import NotFoundError

app,rt,todos,Todo = fast_app(
    'data/todos.db',
    id=int, title=str, pk='id')

def tid(id): return f'todo-{id}'


@app.delete("/delete_todo", name='delete_todo')
async def delete_todo(id:int): 
    try: todos.delete(id)
    except NotFoundError: pass # If someone else deleted it already we don't have to do anything

@patch
def __ft__(self:Todo):
    show = Strong(self.title, target_id='current-todo')
    delete = A('delete',
               hx_delete=delete_todo.to(id=self.id).lstrip('/'), 
               hx_target=f'#{tid(self.id)}',
               hx_swap='outerHTML')
    return Li(show, ' | ', delete, id=tid(self.id))

def mk_input(**kw): return Input(id="new-title", name="title", placeholder="New Todo", **kw)

@rt
async def index():
    add =  Form(Group(mk_input(), Button("Add")), 
                post="insert_todo", target_id='todo-list', hx_swap="beforeend")
    card = Card(Ul(*todos(), id='todo-list'), header=add, footer=Div(id='current-todo')),
    title = 'Todo list'
    return Title(title), Main(H1(title), card, cls='container')

@rt
async def insert_todo(todo:Todo): return todos.insert(todo), mk_input(hx_swap_oob='true')

serve()
EOF

poetry run python main.py

Any Comments ?

sha256: d920811503afe4ef9a1e579531cfaa5a7694082b66547ea9a24c77dac005876a

OpenBSD 7.4 DevBox

OpenBSD 7.4

… will be released next week (23. Oct 2023). Why not have a look at the upcomming OS and prepare a VM for Software Development ?

Preparation

grab a fresh VM and Install OpenBSD 7.4

os version

puffy74# sysctl kern.version
kern.version=OpenBSD 7.4 (GENERIC.MP) #1396: Sun Oct  8 09:20:40 MDT 2023
    [email protected]:/usr/src/sys/arch/amd64/compile/GENERIC.MP

empty vm

puffy74# pkg_info
quirks-6.159        exceptions to pkg_add rules and cache

add go, rust, python

puffy74# pkg_add go rust python3
quirks-6.159 signed on 2023-10-07T10:09:24Z
go-1.21.1: ok
rust-1.72.1:nghttp2-1.56.0: ok
rust-1.72.1:nghttp3-0.15.0: ok
rust-1.72.1:ngtcp2-0.19.1: ok
rust-1.72.1:curl-8.3.0p0: ok
rust-1.72.1:libssh2-1.11.0: ok
rust-1.72.1: ok
python3-3.10p2:xz-5.4.4: ok
python3-3.10p2:sqlite3-3.42.0: ok
python3-3.10p2:libiconv-1.17: ok
python3-3.10p2:gettext-runtime-0.22.2: ok
python3-3.10p2:libffi-3.4.4: ok
python3-3.10p2:bzip2-1.0.8p0: ok
python3-3.10p2:python-3.10.13: ok
python3-3.10p2: ok

show packages

Alpine - Pandas on Docker Image

How to install Pandas on Alpine Linux

Run Alpine Container

docker run -it alpine

add packages

apk update
apk add python3 py3-pip gcc python3-dev g++

add / build pandas

time pip install pandas
real 26m 13.14s
user 30m 46.40s
sys   3m 27.51s

Happy Pandas !


Any Comments ?

sha256: afb99c7e3ed003bee48b65795a153c4fe7835fe3dae0759b70ab2bfb5adc4fd5

Python

Python Snippets

RealPython Best Practices: https://realpython.com/tutorials/best-practices/

Flush Stdout

import sys
from time import sleep

for i in range(1, 101):
    print(".", end="")
    sys.stdout.flush()
    sleep(0.01)

Remove a substring from the end of a string

url = "abcdc.com"
url.removesuffix(".com")  # Returns 'abcdc'
url.removeprefix("abcdc.")  # Returns 'com'

or

url = "abcdc.com"
if url.endswith(".com"):
    url = url[:-4]

or regex

import re

url = "abcdc.com"
url = re.sub("\.com$", "", url)

Modul ‘ping3’

echo "# allow all users to create icmp sockets" > /etc/sysctl.d/ping_group.conf
echo "net.ipv4.ping_group_range=0 2147483647"   > /etc/sysctl.d/ping_group.conf
sysctl net.ipv4.ping_group_range='0   2147483647'

Convert CSV to JSON

cat LARGEFILE.csv |python3 -c 'import csv, json, sys; print(json.dumps([dict(r) for r in csv.DictReader(sys.stdin)]))' > LARGEFILE.json

Show System Path

python3.10 -c "import sys; print('\n'.join(sys.path))"

Zfill

Padding Zero