Python HTML generator

Dominate is an HTML generation library that lets you easily create tags. In dominate, python reserved words are prefixed with an underscore, so it would look like this: from dominate.tags import * t = div(table(_id=”the_table”), _class=”tbl”) print(t) <div class=”tbl”> <table id=”the_table”></table> </div> Disclaimer: I am the author of dominate

Update row (SQLAlchemy) with data from marshmallow

UPDATED, 2022-12-08 Extending the ModelSchema from marshmallow-sqlalchemy instead of Flask-Marshmallow you can use the load method, which is defined like this: load(data, *, session=None, instance=None, transient=False, **kwargs) Putting that to use, it should look like that (or similar query): node_schema.load(json_data, session= current_app.session, instance=Node().query.get(node_id)) And if you want to load without all required fields of Model, … Read more

How to print the progress of a list comprehension in python?

tqdm Using the tqdm package, a fast and versatile progress bar utility pip install tqdm from tqdm import tqdm def process(token): return token[‘text’] l1 = [{‘text’: k} for k in range(5000)] l2 = [process(token) for token in tqdm(l1)] 100%|███████████████████████████████████| 5000/5000 [00:00<00:00, 2326807.94it/s] No requirement 1/ Use a side function def report(index): if index % 1000 … Read more

How can I get headers or a specific header from my backend API?

It’s pretty similar, you can do from fastapi import FastAPI, Request @app.get(“/”) async def root(request: Request): my_header = request.headers.get(‘header-name’) … NOTE: that it’s lowercased Example: from fastapi import FastAPI, Request app = FastAPI() @app.get(“/”) async def root(request: Request): my_header = request.headers.get(‘my-header’) return {“message”: my_header} Now if you run this app with uvicorn on your localhost, … Read more

How to show related objects in Django/Admin?

You can use “Inlines” to visualize and edit Things of a certain Category in the admin detail for that category: In the admin.py file, create an Inline object for Thing (ThingInline) and modify your CategoryAdmin class to have an inline of type ThingInline like this: … class ThingInline(admin.TabularInline): model = Thing class CategoryAdmin(admin.ModelAdmin): inlines = … Read more

Python dataclass, what’s a pythonic way to validate initialization arguments?

Define a __post_init__ method on the class; the generated __init__ will call it if defined: from dataclasses import dataclass @dataclass class MyClass: is_good: bool = False is_bad: bool = False def __post_init__(self): if self.is_good: assert not self.is_bad This will even work when the replace function is used to make a new instance.

File not found.