One observation from my side doing some tests 2 weeks ago to understand why redrawing my layout with 10-20 rows (each containing some text, a button and a few plots) takes long to finish redrawing on the browser:
If you have lots of panel.pane.Str, panel.pane.HTML or Markdowns this slows down the redrawing if you do not fully clear the container (eg. Column), then prepare the new content and add all in one go.
Fast: .
my_main_column.extend([new_rows])
my_main_column.objects = new_rows
Slow: anything using append
for row in new_rows:
my_main_column.append(new_row)
Problem doesnât happen with redrawing widgets, bokeh plots etc. just with panel.pane Text stuff.
The code finishes same time, but the browser update takes much longer (and redrawing the text is quite visible). Doesnât matter if MS-EDGE or FIREFOX.
Here some test-code for JupyterLab (but starts a panel server) to separate the display.
import numpy as np
import panel as pn
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
pn.extension()
ui = pn.Column()
def update_layout(
container, buttons=1, plots=3, option=1,
color='red', button_type='danger'
):
container.clear()
if plots > 0:
scatter_data = {'x': np.random.random(100),
'y': np.random.random(100)}
new_layout = []
for row in range(20):
ui_row = pn.Row(
f' STRING {row}',
f'# MARKDOWN {row}',
pn.pane.HTML(f'<H1>HTML {row}</H1>'),
styles={'background': color})
for button in range(buttons):
ui_row.append(
pn.widgets.Button(name=color, button_type=button_type))
for plot in range(plots):
p = figure(title='title', width=200, height=100, tools='hover')
source = ColumnDataSource(scatter_data)
p.scatter(x='x', y='y', color=color, source=source)
ui_row.append(p)
if option == 1:
# option 1: worse performance
container.append(ui_row)
elif option == 2:
# option 2( part1): better performance
new_layout.append(ui_row)
if option == 2:
# option 2 (part2) better performance
container.objects = new_layout
return container
# create layout and server
update_layout(container=ui, buttons=1, plots=3, option=1, color='red', button_type='danger')
ui.show()
# sampel commands to test redrawing behaviour
option = 1 ### 1 ... slow, 2 ... fast
buttons = 0 ## number of buttons to draw
plots = 0 ## number of plots to draw
update_layout(container=ui, buttons=buttons, plots=plots, option=option,
color='red', button_type='danger')
print('done')
# wait and try again
update_layout(container=ui, buttons=buttons, plots=plots,
option=option, color='lightblue', button_type='warning')
print('done1')