Run wasm built with wasm-pack with pythonmonkey

2025-01-03
I have been trying to run wasm code built with wasm-pack with pythonmonkey, a Mozilla SpiderMonkey JavaScript engine embedded into the Python Runtime. Consider a lib.rs as follow: use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn add(a: i32, b: i32) -> i32 { return a + b; } With wasm-pack, we can build the src code to wasm wasm-pack build --target nodejs --no-typescript We will get a .wasm file and a .js wrapper file. Continue reading

Combine mesop with natural abilities of flask

2024-12-23
mesop is popular with AI apps, since is can be used to build web apps with Python without frontend works. mesop is developed upon the popular frontend framework flask. How about combine the natural abilities of flask while developing with mesop? Let do a little research. Access flask request instance(including headers, cookies) In any mesop page, you may simply access flask.request, flask.request.cookies, flask.request.headers by importing flask. For example import flask import mesop as me @me. Continue reading

Create a partial model definition of a pydantic model

2024-12-09
Sometimes I want to create a partial model definition of a pydantic model. For example, if I want to expose a database model from API, some of the fields I would like to keep in secret, or I just want to expose very few fields in a summary API. I don’t want to write model definition twice. I would like to write something like ModelExposed = partial_model(ModelSource, 'ModelExposed', include=["title", "description"]) So I write a funciton as below to do it, including support of passing list of excluding fields, copying computed fields and validators. Continue reading

psql vs. mysql shell command cheatsheet

2024-12-05
MySQL Postgres list databases SHOW DATABASES \l change to a database USE <db_name> \c <db_name> list tables SHOW TABLES \dt describe a table DESC <table_name> \d <table_name> show the create table sql SHOW CREATE TABLE <table_name> pg_dump -st <table_name> <db_name> explain a query EXPLAIN <sql_statement> EXPLAIN <sql_statement> expanded(vertical) display \G \gxor use \x to switch get help ? <SQL STATEMENT>HELP <SQL STATEMENT> \h <SQL STATEMENT> get active processes SHOW [FULL] PROCESSLIST; SELECT pid, usename, state, query, query_start, application_name FROM pg_stat_activity WHERE state = ‘active’; kill a process KILL <pid>; SELECT pg_terminate_backend(<pid>); quit shell QUIT \q

Deploy next.js using pm2 cluster mode(and with docker)

2024-09-03
We can simply start the Next.js application using next start. However, when facing higher traffic demands in practical use, how can we deploy Next.js using a multi-process approach? We can achieve this using the cluster mode of pm2. The cluster mode is a powerful way of scaling Node.js applications, allowing you to start multiple processes, all listening on the same port. How do we do this? First, we can create a . Continue reading

Use python -m http.server in SSL

2024-09-02
python -m http.server is a very convenient command line tool to start an ad-hoc static web server. This tool is very useful when you want to develop small web applications or serve static files. But it does not support SSL. A lot of modern web applications as well as browser features require a secured connection. So I want to wrap http.server in SSL and try making it simple to use. Continue reading

The contextvars and the chain of asyncio tasks in Python

2024-08-10
In Python, the contextvars is somehow like thread local, but mostly for coroutines. But will the context itself be kept consistent in a chain of asyncio tasks? Let’s find out. contextvars Let’s have a rough review of the contextvars in Python. The contextvars module provides a way to maintain a context across a chain of function calls, natively supported with asyncio. For example import asyncio import contextvars var = contextvars.ContextVar('var', default={}) async def sub2(): print(f'in sub2, {var. Continue reading
Older posts