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

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

Several tips for better working with Python in Excel

2023-10-10
After enrolled for Python in Excel preview, now I can type =py( in any Excel cell to write some Python code. Python in Excel doesn’t have detailed documents, only Get Started tutorials, like link 1, link 2, or articles talking about topics like pandas, matplotlib, seaborn, etc., it may confuse you when you want to do some real work in an unfamiliar environment. The article notes several tips from my understanding of Python in Excel. Continue reading

Analyze robots.txt with Python Standard Library

2023-09-24
If haven’t searched both “python” and “robots.txt” in the same input box, I would not ever know that Python Standard Library could parse robots.txt with urllib.robotparser. But the official document of urllib.robotparser doesn’t go into detail. With the document, you could check whether a url can be fetch with a robot with robot_parser_inst.can_fetch(user_agent, url) if you are building a crawler bot yourself. But if you want to do some statistics about robots. Continue reading

Process AWS Kinesis Firehose data with Python

2023-09-11
Pipelining streamed data events directly into Amazon S3 via the AWS Kinesis Firehose service is convenient and efficient. And we could use Python with boto3 to consume the data directly from S3. This allows for seamless storage of your data, ensuring its integration and accessibility. Mostly, we are dealing with JSON-formatted event logs. But there is one tiny stone in the shoe for logs feeding from AWS Kinesis Firehose, there is no newline between consecutive log entries. Continue reading
Older posts