Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to implement a progress bar that renders multiple times when a function is running? #1194

Answered by Archmonger
qguo-MCC asked this question in Question
Discussion options

I am trying to understand the basics of progress bar in reactpy. I want the progress bar to update itself multiple times when a button is clicked. I try to implement a dummy example using bootstrap progress bar. The basic idea is that if the "Count" button is clicked, it will run a function that yields a number (0,20,40,60,80,100) every second. I want the progress bar to update for every second. But obviously, my approach is wrong, as the event handler function will only render once after it runs all the codes. How to implement this correctly? Thanks.

: handle_count_button}, "Count"), html.br(), html.br(), html.div( { 'class_name': 'progress', 'role': 'progressbar', 'aria-valuenow':'25', 'aria-valuemin':'0', 'aria-valuemax':'100', 'aria-label': 'Basic example' }, html.div( { 'class_name': 'progress-bar', 'style': {'width': f"{data}%"}, } ) ) )
You must be logged in to vote

There are two common approaches you can take to update a status bar

  1. Store the value in a something like redis or mysql and have your component poll for it via use_effect
@component
def progress_bar_app():
 percentage, set_percentage = hooks.use_state(0)
 
 @hooks.use_effect
 async def update_percentage():
 while True:
 await sleep(1)
 # Retrieve the current percentage from something like a database or redis.
 current = await get_percentage()
 set_percentage(current)
 return ...
  1. Have the percentage be a component prop (get the actual value from a parent component).

This is useful if you have a parent component that is a...

Replies: 1 comment

Comment options

There are two common approaches you can take to update a status bar

  1. Store the value in a something like redis or mysql and have your component poll for it via use_effect
@component
def progress_bar_app():
 percentage, set_percentage = hooks.use_state(0)
 
 @hooks.use_effect
 async def update_percentage():
 while True:
 await sleep(1)
 # Retrieve the current percentage from something like a database or redis.
 current = await get_percentage()
 set_percentage(current)
 return ...
  1. Have the percentage be a component prop (get the actual value from a parent component).

This is useful if you have a parent component that is actually performing the "progression actions".

@component
def progress_bar_app(percentage):
 return ...
@component
def parent():
 return html.div(
 progress_bar_app(percentage=100),
 )
You must be logged in to vote
0 replies
Answer selected by Archmonger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /