{ "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 }