PyWebIO button to refresh the page

Viewed 1635

so I am making a small app, which searches the web via keyword in PyWebIO input() function and then shows results in put_table() and after the results are ready I need to start another search and instead of updating the page with F5 or browser, I want to create a put_button() which will update or get me back to input(), can anyone help, please? Im just not sure what should I put in onlick= attribute in this case.

3 Answers

First, I'd like to quote from the pywebio documentation:

When you encounter a design problem when using PyWebIO, you can ask yourself a question: What would I do if it is in a terminal program?

The solution in terminal program is to put the input() and put_table() into a while loop. So, in pywebio, you can do the same thing:

from pywebio.input import * 
from pywebio.output import * 

while True:
    keyword = input()
    # make some query
    put_table(...)

The table will be appended to the page rather than be updated with this code. So do in terminal program. You can call pywebio.output.clear() after the input() to clear the page or use scope feature.

from pywebio.input import * 
from pywebio.output import * 

while True:
    keyword = input()
    # make some query
    with use_scope('result', clear=True):
        put_table(...)

When you use while True in server mode, you don't need to worry about the session will be running forever, because when you close the broswer tab, the pywebio will raise exception in your session to exit it.

In script mode, if you want to provide a way to exit the loop, you can:

from pywebio.input import * 
from pywebio.output import * 

while True:
    keyword = input()
    # make some query
    with use_scope('result', clear=True):
        put_table(...)
    if actions("Continue?",["continue", "exit"])=="exit":
        break
from pywebio.session import run_js

put_button("ReUpload_images",onclick=lambda: run_js('window.location.reload()'))
Related