Skip to content

Instantly share code, notes, and snippets.

View jfjensen's full-sized avatar

Jes Fink-Jensen jfjensen

View GitHub Profile
@jfjensen
jfjensen / home.py
Last active November 13, 2023 16:47
Brython code that is designed to interact with a Litestar web app to manage a book database
import json
from browser import alert, document, ajax, html
base_url = "http://127.0.0.1:8000/"
def click_get_all_books(event):
get_all_books()
def get_all_books():
url = base_url + "books"
@jfjensen
jfjensen / home.html
Created October 20, 2023 16:54
An HTML webpage for managing a book database
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-exp.min.css">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.11.0/brython.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.11.0/brython_stdlib.js"></script>
</head>
<body onload="brython()">
<div class="container">
@jfjensen
jfjensen / app_dto_sqlite.py
Created October 20, 2023 16:48
A Litestar web application is being set up to manage a collection of books, using SQLAlchemy for data storage and Jinja for rendering a template.
from __future__ import annotations
from dataclasses import dataclass
from contextlib import asynccontextmanager
from litestar.exceptions import HTTPException
from litestar import Litestar, get, post, put, patch, delete
from litestar.dto import AbstractDTO, field, DataclassDTO, DTOConfig, DTOData
from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
@jfjensen
jfjensen / test_app_routehandler.py
Created October 20, 2023 16:34
This code provides a set of test functions to verify the behavior of a Litestar web app
import requests
import logging
logging.getLogger('faker').setLevel(logging.ERROR)
HOST = "http://127.0.0.1"
PORT = "8000"
BASE_URL = HOST + ":" + PORT
def test_list_users():
@jfjensen
jfjensen / app_routehandler.py
Created October 20, 2023 16:27
A simple web application using the Litestar web framework, with basic CRUD operations for managing user data
from dataclasses import dataclass
from litestar import Litestar, get, post, delete
from litestar.exceptions import HTTPException
@dataclass
class User():
user_id: int
name: str
age: int
email: str
@jfjensen
jfjensen / app_datamodel.py
Created October 18, 2023 11:25
An example application that manages a simple in-memory database of books using Litestar for Python
from __future__ import annotations
from dataclasses import dataclass
from litestar import Litestar, post, get, patch, put
from litestar.dto import DataclassDTO, DTOConfig, DTOData
from litestar.exceptions import HTTPException
@dataclass
class Book:
title: str
author: str
@jfjensen
jfjensen / users.html
Created October 18, 2023 10:22
A Jinja template used to dynamically create an HTML table to display a list of users for a Litestar app
<!DOCTYPE html>
<html lang="en">
<head>
<title>User listing</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
@jfjensen
jfjensen / user.html
Created October 18, 2023 10:13
A Jinja template for a simple Litestar app
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Webpage</title>
</head>
<body>
<h1>Hello user: {{ name }}!</h1>
{# a comment #}
</body>
</html>
@jfjensen
jfjensen / app_jinja.py
Created October 18, 2023 10:04
An example using a Jinja template with the Litestar framework for Python
from litestar import Litestar, get
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.response import Template
from litestar.template.config import TemplateConfig
from litestar.exceptions import HTTPException
from pydantic import BaseModel
class User(BaseModel):
user_id: int
name: str
@jfjensen
jfjensen / app.py
Created October 18, 2023 09:56
A basic example of how to use Litestar for Python
from asyncio import sleep
from typing import Any
from litestar import Litestar, get
@get("/async")
async def async_hello_world() -> dict[str, Any]:
await sleep(0.1)
return {"hello": "world"}
@get("/sync", sync_to_thread=False)