File size: 1,881 Bytes
e08e6f8
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: validator_simple"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "\n", "def validate_input(age, location):\n", "    return [\n", "        gr.validate(not age or age > 3, \"Age must be at least 3\"),\n", "        gr.validate(\"london\" not in location.lower(), \"Location must not be in London\"),\n", "    ]\n", "\n", "\n", "def process_text(age, location):\n", "    return f\"Processed: {age} -- {location.upper()}\"\n", "\n", "\n", "with gr.Blocks() as demo:\n", "    gr.Markdown(\"# Validator Parameter Test Demo\")\n", "\n", "    with gr.Row():\n", "        with gr.Column():\n", "            age = gr.Number(\n", "                label=\"Enter age\",\n", "                placeholder=\"Enter age\",\n", "            )\n", "            location = gr.Textbox(\n", "                max_lines=3,\n", "                label=\"Enter location\",\n", "                placeholder=\"Enter location\",\n", "            )\n", "\n", "    validate_btn = gr.Button(\"Process with Validation\", variant=\"primary\")\n", "\n", "    output_with_validation = gr.Textbox(\n", "        label=\"Output (with validation)\", interactive=False\n", "    )\n", "\n", "    validate_btn.click(\n", "        fn=process_text,\n", "        validator=validate_input,\n", "        inputs=[age, location],\n", "        outputs=output_with_validation,\n", "    )\n", "\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}