{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import param\n", "import panel as pn\n", "\n", "pn.extension(sizing_mode=\"stretch_width\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## param.Action Example\n", "\n", "This example demonstrates how to use ``param.Action`` to trigger an update in a method that depends on that parameter. Actions can trigger any function, but if we simply want to trigger a method that depends on that action, then we can define a small ``lambda`` function that triggers the parameter explicitly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class ActionExample(param.Parameterized):\n", " \"\"\"\n", " Demonstrates how to use param.Action to trigger an update.\n", " \"\"\"\n", "\n", " action = param.Action(lambda x: x.param.trigger('action'), label='Click here!')\n", " \n", " number = param.Integer(default=0)\n", " \n", " @param.depends('action')\n", " def get_number(self):\n", " self.number += 1\n", " return self.number\n", " \n", "action_example = ActionExample()\n", "component = pn.Column(\n", " pn.Row(\n", " pn.Column(pn.panel(action_example, show_name=False, margin=0, widgets={\"action\": {\"button_type\": \"primary\"}, \"number\": {\"disabled\": True}}),\n", " '**Click the button** to trigger an update in the output.'),\n", " pn.panel(action_example.get_number, width=300), max_width=600)\n", ")\n", "component" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## App\n", "\n", "Lets wrap it into nice template that can be served via `panel serve action_button.ipynb`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.template.FastListTemplate(\n", " site=\"Panel\", title=\"param.Action Example\", \n", " main=[\n", " \"This example demonstrates **how to use ``param.Action`` to trigger an update** in a method that depends on that parameter.\\n\\nActions can trigger any function, but if we simply want to trigger a method that depends on that action, then we can define a small ``lambda`` function that triggers the parameter explicitly.\",\n", " component,\n", " ]\n", ").servable();" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }