Skip to content

Instantly share code, notes, and snippets.

@pydanny
Created December 28, 2024 00:10
Show Gist options
  • Save pydanny/3bc248038542565ae8151397159f267b to your computer and use it in GitHub Desktop.
Save pydanny/3bc248038542565ae8151397159f267b to your computer and use it in GitHub Desktop.
fasthtml/check bools.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"id": "4d8c5a51",
"cell_type": "markdown",
"source": "# Check bools (#583)"
},
{
"metadata": {},
"id": "a27befba",
"cell_type": "markdown",
"source": "Reviewing [FastHTML issue #583](https://github.com/AnswerDotAI/fasthtml/issues/583), which appears to be a non-issue."
},
{
"metadata": {
"trusted": true
},
"id": "bc97a990",
"cell_type": "code",
"source": "from fasthtml.common import *",
"execution_count": 1,
"outputs": []
},
{
"metadata": {},
"id": "164b8558",
"cell_type": "markdown",
"source": "Debug wrapper from issue description"
},
{
"metadata": {
"trusted": true
},
"id": "47cdd7a0",
"cell_type": "code",
"source": "# Debug wrapper\ndef debug_fast_app(*args, **kwargs):\n print(\"\\nDebugging table schemas:\")\n for k, v in kwargs.items():\n if isinstance(v, dict) and 'pk' in v: # This identifies our table schemas\n print(f\"\\n{k} schema:\")\n for field, type_ in v.items():\n print(f\" {field}: {type_} ({type(type_)})\")\n \n result = fast_app(*args, **kwargs)\n return result",
"execution_count": 2,
"outputs": []
},
{
"metadata": {},
"id": "fea59cd3",
"cell_type": "markdown",
"source": "Result of running debug wrapper"
},
{
"metadata": {
"trusted": true
},
"id": "a253f3e1",
"cell_type": "code",
"source": "app, rt, tasks, Task = debug_fast_app(\n \"data/data.db\",\n live=True,\n task={\n \"id\": int,\n \"title\": str,\n \"done\": bool,\n \"priority\": int,\n \"profile_id\": int,\n \"pk\": \"id\"\n }\n)\n\n",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "\nDebugging table schemas:\n\ntask schema:\n id: <class 'int'> (<class 'type'>)\n title: <class 'str'> (<class 'type'>)\n done: <class 'bool'> (<class 'type'>)\n priority: <class 'int'> (<class 'type'>)\n profile_id: <class 'int'> (<class 'type'>)\n pk: id (<class 'str'>)\n"
}
]
},
{
"metadata": {},
"id": "c8ea43d0",
"cell_type": "markdown",
"source": "Dataclass definition of Task table"
},
{
"metadata": {
"trusted": true
},
"id": "1c685c97",
"cell_type": "code",
"source": "Task",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "fastlite.core.Task"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"id": "160373aa",
"cell_type": "markdown",
"source": "Generated tasks table"
},
{
"metadata": {
"trusted": true
},
"id": "7c6b4dc5",
"cell_type": "code",
"source": "tasks",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": "<Table task (id, title, done, priority, profile_id)>"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"id": "22008046",
"cell_type": "markdown",
"source": "Insert a task record. "
},
{
"metadata": {
"trusted": true
},
"id": "ac9b1c47",
"cell_type": "code",
"source": "task = tasks.insert(\n Task(\n id=1,\n title='Test bools',\n done=False,\n priority=3,\n profile_id=1\n )\n)",
"execution_count": 7,
"outputs": []
},
{
"metadata": {},
"id": "0e25e714",
"cell_type": "markdown",
"source": "Record is saved."
},
{
"metadata": {
"trusted": true
},
"id": "5e562b27",
"cell_type": "code",
"source": "task",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "Task(id=1, title='Test bools', done=0, priority=3, profile_id=1)"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"id": "e05e9c9d",
"cell_type": "markdown",
"source": "Value of False is correctly saved as 0. The `bool` value is interpreted correctly by `apswutils`, `fastlite`, and `fasthtml` as a numeric type with a value of 0. This is how `sqlite` handles True/False values, as 1s and 0s."
},
{
"metadata": {
"trusted": true
},
"id": "4620d8d3",
"cell_type": "code",
"source": "task.done",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "0"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"id": "d11d084e",
"cell_type": "code",
"source": "bool(task)",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"id": "590e9e52",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.12.6",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "fasthtml/check bools.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment