{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "description: Creation of the Gradio GUI for the app\n", "output-file: gradio_app.html\n", "title: gradio_app\n", "\n", "---\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "#| default_exp gradio_app" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'app_starter_gui'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m#| hide\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgradio\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mgr\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mapp_starter_gui\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Button, load_buttons\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mapp_starter_gui\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcss\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m generate_css\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'app_starter_gui'" ] } ], "source": [ "#| export\n", "#| hide\n", "import gradio as gr\n", "from itertools import groupby\n", "from operator import attrgetter\n", "from app_starter_gui.core import Button, load_buttons\n", "from app_starter_gui.css import generate_css" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "#| hide\n", "from nbdev.showdoc import show_doc" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "#| export\n", "def create_app(yaml_file='buttons.yaml', css_generator=generate_css):\n", " \"\"\"Create a Gradio dashboard for the button interface\n", " \n", " Args:\n", " yaml_file: Path to YAML file containing button data (default: 'buttons.yaml')\n", " css_generator: Function to generate CSS (default: generate_css)\n", " Returns:\n", " gr.Blocks: Gradio interface\n", " \"\"\"\n", " buttons = load_buttons(yaml_file)\n", " css = generate_css(buttons)\n", "\n", " buttons_sorted = sorted(buttons, key=attrgetter('app_type'))\n", " button_groups = {k: list(g) for k, g in groupby(buttons_sorted, key=attrgetter('app_type'))}\n", " \n", " with gr.Blocks(css=css) as blocks:\n", " with gr.Row():\n", " # Create a column for each app_type\n", " for app_type, type_buttons in button_groups.items():\n", " with gr.Column():\n", " gr.Markdown(f\"### {app_type}\")\n", " for button_idx, button in enumerate(type_buttons):\n", " btn = gr.Button(\n", " value=button.label,\n", " elem_id=f\"btn_{app_type}_{button_idx}\", # Make unique ID for each button\n", " scale=1,\n", " )\n", " btn.click(\n", " fn=None, \n", " inputs=None, \n", " outputs=None,\n", " js=f\"() => {{ window.open('{button.link_url}', '_blank'); }}\"\n", " )\n", " return blocks" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "# Example usage\n", "app = create_app('test_buttons.yaml')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "#| export\n", "#| hide\n", "from fastcore.test import *\n", "\n", "def test_create_app():\n", " \"Test app creation with example buttons\"\n", " app = create_app('test_buttons.yaml')\n", " test(app, gr.Blocks, isinstance)\n", " \n", "test_create_app()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "#| hide\n", "import nbdev; nbdev.nbdev_export()" ] } ], "metadata": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }