<jupyter_start><jupyter_text>This example creates a random number generator that periodically updates every two seconds, or with a click of a button.This demonstrates how to add a periodic callback, how to link a button and a toggle to a couple callbacks--the former to manually generate a random number and the latter to toggle periodic generation of a random number.<jupyter_code>def generate_random_number(event=None):
    static_text.value = np.random.randint(low=100000, high=200000)

def toggle_periodic_callback(event):
    if event.new is True:
        periodic_cb.start()
    else:
        periodic_cb.stop()
        
def update_period(event):
    periodic_cb.period = event.new

static_text = pn.widgets.StaticText(name='Periodic Random Number Generator',
                                    value='000000')

generate_button = pn.widgets.Button(name='Generate New Number')
generate_button.on_click(generate_random_number)

periodic_toggle = pn.widgets.Toggle(name='Periodically Generate',
                                    value=False, button_type='primary')
periodic_toggle.param.watch(toggle_periodic_callback, 'value')

period = pn.widgets.Spinner(name="Period (ms)", value=500, step=50, start=50)
period.param.watch(update_period, 'value')

periodic_cb = static_text.add_periodic_callback(
    generate_random_number, period=period.value, start=False)  # period in milliseconds

col = pn.Column(generate_button, period, periodic_toggle, static_text)
col.servable()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08874172185430464, 'fraction_numerical': 0.016556291390728478, 'mean_word_length': 4.4548736462093865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '167996', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 455, 'n_tokens_neox': 421, 'n_words': 114, 'path': 'examples/gallery/simple/random_number_generator.ipynb', 'repo_name': 'slamer59/panel', 'size': '1510'}