Spaces:
Running
Running
File size: 4,650 Bytes
403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b d1b6a3a 403335b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"---\n",
"description: Core functionality for button management\n",
"output-file: core.html\n",
"title: core\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| default_exp core"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"#| hide\n",
"from dataclasses import dataclass\n",
"import yaml\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"from nbdev.showdoc import show_doc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"@dataclass\n",
"class Button:\n",
" \"\"\"A button with label, image, link, and type\"\"\"\n",
" label: str\n",
" image_url: str\n",
" link_url: str\n",
" app_type: str"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def save_buttons(buttons, file='buttons.yaml'):\n",
" \"\"\"Save buttons to YAML file\n",
" \n",
" Args:\n",
" buttons: List of Button objects\n",
" file: Path to YAML file (default: 'buttons.yaml')\n",
" \"\"\"\n",
" button_data = [vars(b) for b in buttons]\n",
" with open(file, 'w') as f:\n",
" yaml.safe_dump(button_data, f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def load_buttons(file='buttons.yaml'):\n",
" \"\"\"Load buttons from YAML file\n",
" \n",
" Args:\n",
" file: Path to YAML file (default: 'buttons.yaml')\n",
" Returns:\n",
" List of Button objects\n",
" \"\"\"\n",
" if not Path(file).exists():\n",
" return []\n",
" with open(file) as f:\n",
" button_data = yaml.safe_load(f)\n",
" return [Button(**data) for data in button_data]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example usage\n",
"example_buttons = [\n",
" Button(\n",
" label=\"ChatGPT\",\n",
" image_url=\"https://example.com/chatgpt.jpg\",\n",
" link_url=\"https://chat.openai.com\",\n",
" app_type=\"AI\"\n",
" ),\n",
" Button(\n",
" label=\"Google\",\n",
" image_url=\"https://example.com/google.jpg\",\n",
" link_url=\"https://google.com\",\n",
" app_type=\"Search\"\n",
" )\n",
"]\n",
"\n",
"# Save example buttons\n",
"save_buttons(example_buttons, \"test_buttons.yaml\")\n",
"\n",
"# Load and test\n",
"loaded_buttons = load_buttons(\"test_buttons.yaml\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"#| hide\n",
"from fastcore.test import *\n",
"\n",
"def test_save_load():\n",
" \"Test that buttons save and load correctly\"\n",
" test_buttons = [\n",
" Button(\"Test1\", \"img1.jpg\", \"http://test1.com\", \"Type1\"),\n",
" Button(\"Test2\", \"img2.jpg\", \"http://test2.com\", \"Type2\")\n",
" ]\n",
" save_buttons(test_buttons, \"test_buttons.yaml\")\n",
" loaded_buttons = load_buttons(\"test_buttons.yaml\")\n",
" test_eq(len(loaded_buttons), len(test_buttons))\n",
" test_eq(loaded_buttons[0].label, \"Test1\")\n",
" test_eq(loaded_buttons[0].app_type, \"Type1\")\n",
" test_eq(loaded_buttons[1].label, \"Test2\")\n",
" test_eq(loaded_buttons[1].app_type, \"Type2\")\n",
"\n",
"test_save_load()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"Path(\"test_buttons.yaml\").unlink(missing_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"import nbdev; nbdev.nbdev_export()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|