# Possible issues with styling when updating tabulator

**URL:** <https://discourse.holoviz.org/t/possible-issues-with-styling-when-updating-tabulator/2866>\
**Category:** Panel\
**Tags:** panel, tabulator\
**Created:** [September 27, 2021, 8:39pm UTC](https://discourse.holoviz.org/t/possible-issues-with-styling-when-updating-tabulator/2866 "2021-09-27T20:39:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kadek](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/kadek/32/2595_2.png) [@Kadek](https://discourse.holoviz.org/u/Kadek)\
**Post date:** [September 27, 2021, 8:39pm UTC](https://discourse.holoviz.org/t/possible-issues-with-styling-when-updating-tabulator/2866/1 "2021-09-27T20:39:46Z")

</div>

Hello! I have just discovered the amazing capabilities of tabulator together with pandas styling. Though, I am encountering a lot of quirky behaviour when periodically updating data and wanted to double check here on what’s the correct approach to the problem.

Following this tutorial [Tabulator — Panel 0.12.4rc3 documentation](https://pyviz-dev.github.io/panel/reference/widgets/Tabulator.html#styling)  
I create a simple tabulator that will be periodically updated.

```auto
import datetime as dt
import numpy as np
import pandas as pd
import panel as pn

df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
styled = pn.widgets.Tabulator(df, page_size=5)

def highlight_max(s):
    '''
    highlight the maximum in a Series yellow.
    '''
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

styled.style.apply(highlight_max)
styled.servable()

pn.state.add_periodic_callback(update, period=1000)

```

The update function looks as follows for:  
A) Using stream

```auto
def update():
    df = pd.DataFrame(np.random.randn(1, 5), columns=list('ABCDE'))
    styled.stream(df)

```

B) Using patch

```auto
def update():
    df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
    styled.patch(df)

```

C) Using value substitution

```auto
def update():
    df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
    styled.value = df

```

The problems I am encountering are:

- When using A and C the styling gets updated according to the new data. In case of B the styling disappears after first update.
- If we set the height to somehing small like 200 we can see the scrollbar. Its position gets resetted to the top on every update. In case of A the follow=true so the scrollbar keeps jumping up and down. Is there some configuration that would just leave the scrollbar in its place? And in the case of stream just follow without first jumping?
- In every method the user selection for sorting columns gets resetted. (I mean here the arrows near the name of the column)
- I am trying to use pandas background gradient to simulate a heatmap [Table Visualization — pandas 1.3.3 documentation](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Background-Gradient-and-Text-Gradient) By injecting below code into example above

```auto
import seaborn as sns
cm = sns.light_palette("green", as_cmap=True)
styled.style.background_gradient(cmap=cm)
styled.servable()

```

This is really cool but just like the in the first example it updates only in cases of A and C. There is also one annoying thing. You can see the whole table is redrawn and it keeps flashing with white color in between. It’s a small thing but it would be very annoying for an end user. Is it possible to somehow remove that effect? For this purpose, in bokeh, I would use normally a figure made of rects to create a heatmap. It doesn’t have this problem and nicely switches between colors.

- Another great thing that seems to be possible here is usage of data bars (section on pandas documentation below the one I linked above, cant post more than two links 😅) This also has problem with flashing but additionally the bars seem to mess up the widths of the columns and the header gets misaligned from the body of the table. I am getting such effect with this code

```auto
styled.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])

```

Overall, thank you so much for the hard work and making this possible! I am happy to drop a PR with fixes myself if I get some reference PR to learn how to fix this stuff. Being able to just push dataframe style to the frontend and have everything running on bokeh server is simply amazing and I am really eager to make it work perfectly!

---

<div class="post-metadata">

**Author:** ![Marc](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/marc/32/24_2.png) [@Marc](https://discourse.holoviz.org/u/Marc)\
**Post date:** [September 28, 2021, 4:16am UTC](https://discourse.holoviz.org/t/possible-issues-with-styling-when-updating-tabulator/2866/2 "2021-09-28T04:16:01Z")

</div>

Hi @Kadek

Could you check if the problems are there in the just released Panel v 0.12.4? I believe it fixes some of these problems.

See [https://github.com/holoviz/panel/releases/tag/v0.12.4](https://github.com/holoviz/panel/releases/tag/v0.12.4).

Then it would be helpful if you report the remaining issues individually with minimum reproducible code and some mp4/ gif video.

It should make it easy to guide a PR or find a fix.

Thanks

---

<div class="post-metadata">

**Author:** ![Kadek](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/kadek/32/2595_2.png) [@Kadek](https://discourse.holoviz.org/u/Kadek)\
**Post date:** [March 18, 2022, 1:54pm UTC](https://discourse.holoviz.org/t/possible-issues-with-styling-when-updating-tabulator/2866/3 "2022-03-18T13:54:11Z")

</div>

Hey @Marc !

I have finally had time to come back to this! I tested those issues with the newest pre-release of panel 0.13.0 and the issue of styling disappearing has been solved.

Unfortunately, other problems are still there. I have created some Github issues to track them:

> <https://github.com/holoviz/panel/issues/3248>
>
> \#### ALL software version info
> Python 3.8.8
> panel 0.12.6
> 
> \#### Description o…f expected behavior and the observed behavior
> Expected behaviour: When patching data in Tabulator the styling should adapt to new data.
> Observed behaviour: When using patch function on tabulator whole styling disappears
> 
> \#### Complete, minimal, self-contained example code that reproduces the issue
> 
> \`\`\`
> import numpy as np
> import pandas as pd
> import panel as pn
> 
> df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
> styled = pn.widgets.Tabulator(df, height=500)
> 
> def highlight\_max(s):
> '''
> highlight the maximum in a Series yellow.
> '''
> is\_max = s == s.max()
> return \['background-color: yellow' if v else '' for v in is\_max\]
> 
> styled.style.apply(highlight\_max)
> 
> 
> def start\_updating(\*args):
> pn.state.add\_periodic\_callback(update, period=1000)
> 
> def update():
> df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
> styled.patch(df)
> 
> button = pn.widgets.Button(name="Start")
> button.on\_click(start\_updating)
> 
> pn.Column(button, styled).servable()
> \`\`\`
> 
> \#### Stack traceback and/or browser JavaScript console output
> 
> \#### Screenshots or screencasts of the bug in action
> 
> https://user-images.githubusercontent.com/2953645/158997659-74a8487f-17bf-43af-9287-12c160a5a921.mp4

> <https://github.com/holoviz/panel/issues/3249>
>
> \#### ALL software version info
> Python 3.8.8
> panel 0.13.0a43
> 
> \#### Descriptio…n of expected behavior and the observed behavior
> 
> Expected behaviour: When patching data in Tabulator the vertical scrollbar should stay in the same position.
> Observed behaviour: When using patch function on Tabulator the vertical scrollbar jumps to the top
> 
> \#### Complete, minimal, self-contained example code that reproduces the issue
> 
> \`\`\`
> import numpy as np
> import pandas as pd
> import panel as pn
> 
> df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
> styled = pn.widgets.Tabulator(df, height=200)
> 
> def highlight\_max(s):
> '''
> highlight the maximum in a Series yellow.
> '''
> is\_max = s == s.max()
> return \['background-color: yellow' if v else '' for v in is\_max\]
> 
> styled.style.apply(highlight\_max)
> 
> 
> def start\_updating(\*args):
> pn.state.add\_periodic\_callback(update, period=1000)
> 
> def update():
> df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
> styled.patch(df)
> 
> button = pn.widgets.Button(name="Start")
> button.on\_click(start\_updating)
> 
> pn.Column(button, styled).servable()
> 
> \`\`\`
> 
> \#### Stack traceback and/or browser JavaScript console output
> 
> \#### Screenshots or screencasts of the bug in action
> 
> https://user-images.githubusercontent.com/2953645/159003446-e99ebde0-53c4-4bcf-a417-8a3c109c865c.mp4

> <https://github.com/holoviz/panel/issues/3250>
>
> \#### ALL software version info
> Python 3.8.8
> panel 0.13.0a43
> 
> \#### Descriptio…n of expected behavior and the observed behavior
> 
> Expected behaviour: Using Bar Charts from pandas styling should result in the same visuals as the reference page shows https://pandas.pydata.org/pandas-docs/stable/user\_guide/style.html#Bar-charts
> 
> Observed behaviour: The column using Bar Chart has different width than its header. Additionally, it seems the width of the table changes slightly on every stream of a new point.
> 
> \#### Complete, minimal, self-contained example code that reproduces the issue
> 
> \`\`\`
> import numpy as np
> import pandas as pd
> import panel as pn
> 
> df = pd.DataFrame(np.random.randn(1, 5), columns=list('ABCDE'))
> styled = pn.widgets.Tabulator(df, height=500)
> 
> styled.style.bar(subset=\['A'\], color=\['#d65f5f'\])
> 
> 
> def start\_updating(\*args):
> pn.state.add\_periodic\_callback(update, period=1000)
> 
> def update():
> df = pd.DataFrame(np.random.randn(1, 5), columns=list('ABCDE'))
> styled.stream(df)
> 
> button = pn.widgets.Button(name="Start")
> button.on\_click(start\_updating)
> 
> pn.Column(button, styled).servable()
> \`\`\`
> 
> \#### Stack traceback and/or browser JavaScript console output
> 
> \#### Screenshots or screencasts of the bug in action
> 
> https://user-images.githubusercontent.com/2953645/159011293-ef60f7b2-4714-4bc1-9978-2cf98066f263.mp4

> <https://github.com/holoviz/panel/issues/3251>
>
> \#### ALL software version info
> Python 3.8.8
> panel 0.13.0a43
> 
> \#### Descriptio…n of expected behavior and the observed behavior
> Expected behaviour: When updating values in Tabulator the new styling should update directly to new values resulting the following visual effect
> 
> https://user-images.githubusercontent.com/2953645/159013660-6ecd81c5-5442-4192-a8d4-c41c9745d40b.mp4
> 
> Observed behaviour: When updating values in Tabulator the table first gets all white and only then shows new colors. This produces annoying flashing effect.
> 
> https://user-images.githubusercontent.com/2953645/159013796-3b4623cd-0175-4d4a-b64e-ce89b4cb6c07.mp4
> 
> Both videos are produced from the code below. The one with flashing colors is produced using 60 FPS. The one without is produced using 30 FPS. The flashing is still there but simply not being caught by recording.
> 
> \#### Complete, minimal, self-contained example code that reproduces the issue
> 
> \`\`\`
> import numpy as np
> import pandas as pd
> import panel as pn
> import seaborn as sns
> 
> df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
> styled = pn.widgets.Tabulator(df, height=500)
> 
> cm = sns.light\_palette("green", as\_cmap=True)
> styled.style.background\_gradient(cmap=cm)
> 
> def start\_updating(\*args):
> pn.state.add\_periodic\_callback(update, period=1000)
> 
> def update():
> df = pd.DataFrame(np.random.randn(10, 5), columns=list('ABCDE'))
> styled.value = df
> 
> button = pn.widgets.Button(name="Start")
> button.on\_click(start\_updating)
> 
> pn.Column(button, styled).servable()
> 
> \`\`\`
> 
> \#### Stack traceback and/or browser JavaScript console output
> 
> Looking at the browser profile (I am using Brave here but can see the same issue in Firefox or Chrome) I can see that on every update we clean the whole table and then draw new grid. It seems making those two operations somehow atomic would solve the problem.
> 
> \<img width="850" alt="image" src="https://user-images.githubusercontent.com/2953645/159014695-a958fd56-e3f7-4640-9b82-2eddd6870a06.png"\>
> 
> 
> \#### Screenshots or screencasts of the bug in action

I would love to dig myself into the code, learn how all of this works and maybe even fix something. Are you aware of any PRs that solved similar issue and could be used by me as a reference as to where to start looking?
