Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example cdsdashboards Panel app including user detection
import panel as pn
import param
import ast
import json
HSPACER = pn.Spacer(height=4, background="#261342")
pn.extension("tabulator")
class EmployeeOverviewAdminRoot(param.Parameterized):
"""Employee Overview App for Administrators"""
employee_id = 0
employees = ["dan", "bob"]
user = param.String()
employee_list = param.ObjectSelector()
def __init__(self, user, **params):
super().__init__(user=user, **params)
self.param.employee_list.objects = list(
self.employees
)
self.param.user = user
try:
self.employee_id = self.employees.index(user)+1
self.employee_list = user
except ValueError:
pass
def layout(self):
"""Layout of the entire app"""
return pn.Column(
self.param.employee_list,
self.get_employee_view(),
sizing_mode="stretch_width",
)
@param.depends("employee_list")
def get_employee_view(self):
if self.employee_id > 0:
employee_status = f"You are employee number {self.employee_id}"
else:
employee_status = "You are not a known employee"
return pn.Column(
f"## Selected Employee: {self.employee_list}",
HSPACER,
pn.pane.Str(employee_status),
f"## You Are: {self.user}",
HSPACER,
sizing_mode="stretch_width",
)
def panel(self):
return pn.panel(self.layout, loading_indicator=True)
def employee_overview_admin(doc):
pn.extension(loading_spinner="dots", loading_color="#00aa41")
user = 'unknown'
header_name = "X-Cdsdashboards-Jh-User"
if header_name in doc.session_context.request.headers:
user = json.loads(doc.session_context.request.headers[header_name]).get('name', user)
app = EmployeeOverviewAdminRoot(user)
layout = app.panel()
if layout is None:
layout = pn.Column("App did not load")
# add the layout to the doc
doc.add_root(layout.get_root())
employee_overview_admin(pn.state.curdoc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment