File size: 5,610 Bytes
403335b
 
 
d1b6a3a
403335b
 
d1b6a3a
 
 
 
 
 
 
403335b
 
 
 
d1b6a3a
9c717aa
 
 
 
 
403335b
 
 
 
 
 
 
d1b6a3a
9c717aa
 
 
 
 
403335b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c717aa
 
403335b
 
 
 
 
 
 
9c717aa
 
 
 
 
403335b
 
 
 
 
 
 
 
 
9c717aa
 
 
 
 
403335b
 
 
 
 
 
 
 
 
 
 
 
 
 
9c717aa
 
 
403335b
 
9c717aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403335b
 
 
 
 
 
9c717aa
 
 
 
 
403335b
 
 
 
 
 
 
 
 
9c717aa
 
 
 
 
403335b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c717aa
 
 
 
 
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
{
 "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
}