anant
1
How do I redirect to a link when I click on a button, based on a few conditions?
In my code, nothing is happening when I click on the button.
Below is my code. I am using panel version 1.23. Please help me with the fix in my code.
Any help will be much appreciated. Thanks!
check_master_table_button = pn.widgets.Button(name="Redirect", button_type="primary", width=250, sizing_mode="fixed")
def redirect_func(event):
region_name = region_name_box.value
url = None
if env == 'prod':
url = f"https://{region_name}.com/"
else:
url = f"https://{region_name}.hello.com"
if url:
check_master_table_button.js_on_click(args=dict(url=url), code="""
window.location.href = url;
""")
redirect_button.on_click(redirect_func)
Couldn’t find a way to unwatch the existing js_on_click so I recreated the button on every change
function way
import param
import panel as pn
pn.extension()
env_param = param.Selector(default="prod", objects=["prod", "dev"])
env_box = pn.widgets.Select(
name="Region Name", value=env_param.default, options=env_param.objects
)
placeholder = pn.pane.Placeholder()
def redirect_func(env):
if env == "prod":
url = "https://panel.holoviz.org/"
else:
url = "https://holoviz-dev.github.io/panel/"
button = pn.widgets.Button(name=f"Open {env}")
button.js_on_click(args=dict(url=url), code="window.open(url, '_blank');")
placeholder.update(button)
pn.bind(redirect_func, env=env_box.param.value, watch=True)
env_box.param.trigger("value")
view = pn.Row(env_box, placeholder)
view.show()
param class way
import param
import panel as pn
pn.extension()
class EnvViewer(pn.viewable.Viewer):
env = param.Selector(default="prod", objects=["prod", "dev"])
def __init__(self, **params):
super().__init__(**params)
self.env_box = pn.widgets.Select.from_param(self.param.env)
self.placeholder = pn.pane.Placeholder()
self.view = pn.Row(self.env_box, self.placeholder)
pn.bind(self.redirect_func, env=self.param.env, watch=True)
self.param.trigger("env")
def redirect_func(self, env):
if env == "prod":
url = "https://panel.holoviz.org/"
else:
url = "https://holoviz-dev.github.io/panel/"
button = pn.widgets.Button(name=f"Open {env}")
button.js_on_click(args=dict(url=url), code="window.open(url, '_blank');")
self.placeholder.update(button)
def __panel__(self):
return self.view
env_viewer = EnvViewer()
env_viewer.show()