Tdqm text color change

Hi , everyone and Merry Christmas to you. I want to know how to change Tqdm text color. in my question, I can change text color. but next step , this text color return to normal(‘black’) . I want to remain this color. What should I do?

Screen shot [ First Step : text color is black ]

Screen shot [ 2nd Step : text color is green that I want to do. ]

Screen shot [ 3rd Step : text color is red that I want to do ]

Screen shot [4th Step : Return from red to black . that is not I wanted]

My simple code

from logging import _STYLES, StrFormatStyle
import time

import numpy as np
import pandas as pd
import panel as pn

from panel.widgets import Tqdm

pn.extension()

tqdm_outer = Tqdm()
tqdm_inner = Tqdm(margin=(0, 0, 0, 20))
tqdm_outer.text_pane.style={'color': 'green'}
tqdm_inner.text_pane.style={'color': 'red'}

def run_nested_loop(*events, timeout=0.05):
    for i in tqdm_outer(range(10)):
        tqdm_outer.text_pane.style={'color': 'green'}

        for j in tqdm_inner(range(10)):
            tqdm_inner.text_pane.style={'color': 'red'}

            time.sleep(timeout)
            
run_nested_loop(timeout=0.001)

button = pn.widgets.Button(name="Run Nested Loop", button_type="success")
button.on_click(run_nested_loop)

app = pn.Column(tqdm_outer, tqdm_inner,button)

# ----------------------------------------------------------------------
#   Main Function
# ----------------------------------------------------------------------
pn.serve(app)

Could anyone tell me my easy question?

Best regards,

1 Like

Hi @kazux68k

I can see your use case has not been anticipated or there is a bug in the Panel code.

The lines of code marked changes your color

You can fix it using the code below.

import time

import panel as pn

# **** Hack Fix ****

class ptqdm2(pn.widgets.indicators.ptqdm):
    def display(self, msg=None, pos=None, bar_style=None):
        super().display(msg, pos)
        style = self._indicator.text_pane.style or {}
        if not "color" in style:
            color = self.colour or 'black'
            self._indicator.text_pane.style = dict(style, color=color)
        if self.total is not None and self.n is not None:
            self._indicator.max = int(self.total) # Can be numpy.int64
            self._indicator.value = int(self.n)
            self._indicator.text = self._to_text(**self.format_dict)
        return True
pn.widgets.indicators.ptqdm=ptqdm2

# **** Hack Fix ****

from panel.widgets import Tqdm

pn.extension()

tqdm_outer = Tqdm()
tqdm_inner = Tqdm(margin=(0, 0, 0, 20))
tqdm_outer.text_pane.style={'color': 'green'}
tqdm_inner.text_pane.style={'color': 'red'}

def run_nested_loop(*events, timeout=0.05):
    for i in tqdm_outer(range(10)):
        for j in tqdm_inner(range(10)):
            time.sleep(timeout)
            
run_nested_loop(timeout=0.001)

button = pn.widgets.Button(name="Run Nested Loop", button_type="success")
button.on_click(run_nested_loop)

app = pn.Column(tqdm_outer, tqdm_inner,button)

app.servable()
1 Like

I’ve filed it as a bug TQDM removes text color set by user via style parameter · Issue #3039 · holoviz/panel (github.com) and put in a PR fixes #3039: tqdm style color reset by MarcSkovMadsen · Pull Request #3040 · holoviz/panel (github.com)

2 Likes

@Marc Thank you for your quick response and teach me solution. I will try it later. Have a happy Merry Christmas

1 Like

@Marc Thank you for your cooperation. I tried it. It works that I wanted. Let me show this screen shot.

Screen shot

20211227_001

How ever , in my condition . I have to delete super function. when super function execute , color return to black. this is the point that I comment outed line.

Screen shot

Is this need super function?