示例#1
0
def test_server_async_callbacks():
    button = Button(name='Click')

    counts = []

    async def cb(event, count=[0]):
        import asyncio
        count[0] += 1
        counts.append(count[0])
        await asyncio.sleep(1)
        count[0] -= 1

    button.on_click(cb)

    server = serve(button, port=5008, threaded=True, show=False)

    # Wait for server to start
    time.sleep(1)

    requests.get("http://localhost:5008/")

    doc = list(button._models.values())[0][0].document
    with set_curdoc(doc):
        for _ in range(5):
            button.clicks += 1

    # Wait for callbacks to be scheduled
    time.sleep(2)

    # Ensure multiple callbacks started concurrently
    try:
        assert max(counts) > 1
    finally:
        server.stop()
示例#2
0
def test_button(document, comm):
    button = Button(name='Button')

    widget = button.get_root(document, comm=comm)

    assert isinstance(widget, button._widget_type)
    assert widget.label == 'Button'

    button._process_events({'clicks': 1})
    assert button.clicks == 1
示例#3
0
def test_button_jscallback_clicks(document, comm):
    button = Button(name='Button')
    code = 'console.log("Clicked!")'
    button.jscallback(clicks=code)

    widget = button.get_root(document, comm=comm)
    assert len(widget.js_event_callbacks) == 1
    callbacks = widget.js_event_callbacks
    assert 'button_click' in callbacks
    assert len(callbacks['button_click']) == 1
    assert code in callbacks['button_click'][0].code
示例#4
0
def test_button(document, comm):
    button = Button(name='Button')

    widget = button._get_root(document, comm=comm)

    assert isinstance(widget, button._widget_type)
    assert widget.clicks == 0
    assert widget.label == 'Button'

    widget.clicks = 1
    button._comm_change({'clicks': widget.clicks})
    assert button.clicks == 1
示例#5
0
def test_button(document, comm):
    button = Button(name='Button')

    box = button._get_model(document, comm=comm)

    assert isinstance(box, WidgetBox)

    widget = box.children[0]
    assert isinstance(widget, button._widget_type)
    assert widget.clicks == 0
    assert widget.label == 'Button'

    widget.clicks = 1
    button._comm_change({'clicks': widget.clicks})
    assert button.clicks == 1
示例#6
0
def test_button_event(document, comm):
    button = Button(name='Button')

    widget = button.get_root(document, comm=comm)

    events = []
    def callback(event):
        events.append(event.new)

    button.param.watch(callback, 'value')

    assert button.value == False
    button._server_click(document, widget.ref['id'], None)
    assert events == [True]
    assert button.value == False
示例#7
0
def test_server_thread_pool_periodic(threads):
    button = Button(name='Click')

    counts = []

    def cb(count=[0]):
        count[0] += 1
        counts.append(count[0])
        time.sleep(0.5)
        count[0] -= 1

    port = 6017
    serve(button, port=port, threaded=True, show=False)

    # Wait for server to start
    time.sleep(1)

    requests.get(f"http://localhost:{port}/")

    doc = list(button._models.values())[0][0].document
    with set_curdoc(doc):
        state.add_periodic_callback(cb, 100)

    # Wait for callbacks to be scheduled
    time.sleep(1)

    # Checks whether periodic callbacks were executed concurrently
    assert max(counts) >= 2
def _fast_button_card():
    button = Button(name="Click me", button_type="primary")
    button.param.name.precedence = 0
    button.param.clicks.precedence = 0
    button.param.disabled.precedence = 0
    button.param.button_type.precedence = 0
    button_parameters = [
        "name",
        "button_type",
        "clicks",
        "disabled",
        "width",
        "height",
        "sizing_mode",
    ]
    settings = Param(
        button,
        parameters=button_parameters,
        show_name=False,
        sizing_mode="stretch_width",
    )
    return Column(
        HTML("<h2>Button</h2>"),
        button,
        HTML("<h3>Parameters</h3>"),
        settings,
        sizing_mode="stretch_both",
    )
示例#9
0
def test_button_event(document, comm):
    button = Button(name='Button')

    widget = button.get_root(document, comm=comm)

    events = []

    def callback(event):
        events.append(event.new)

    button.param.watch(callback, 'value')

    assert button.value == False
    button._process_event(ButtonClick(widget))
    assert events == [True]
    assert button.value == False
示例#10
0
    def app(count=[0]):
        button = Button(name='Click')
        def onload():
            count[0] += 1
            counts.append(count[0])
            time.sleep(2)
            count[0] -= 1

        state.onload(onload)

        # Simulate rendering
        def loaded():
            state._schedule_on_load(None)
        state.curdoc.add_next_tick_callback(loaded)

        return button

class FastButton(Button):
    appearance = param.ObjectSelector(
        default=DEFAULT_FAST_BUTTON_APPEARANCE,
        objects=FAST_BUTTON_APPEARENCES,
        doc="The appearance attribute",
        allow_None=True,
    )
    _rename = {
        **pn.widgets.Button._rename,
        "appearance": None,
    }

    # def __init__(self, **params):
    #     if "button_type" in params and "appearance" not in params:
    #         params["appearance"] = BUTTON_TYPE_TO_APPEARANCE[params["button_type"]]
    #     super().__init__(**params)

    # @param.depends("button_type", watch=True)
    # def _update_accent(self, *_):
    #     self.appearance = BUTTON_TYPE_TO_APPEARANCE[self.button_type]


button = Button(name="Hello World")

pn.Column(
    button,
    pn.Param(button, parameters=["button_type", "clicks", "appearance"]),
).show(port=5007)
示例#12
0
def test_server_thread_pool_change_event(threads):
    button = Button(name='Click')
    button2 = Button(name='Click')

    counts = []

    def cb(event, count=[0]):
        count[0] += 1
        counts.append(count[0])
        time.sleep(0.5)
        count[0] -= 1

    button.on_click(cb)
    button2.on_click(cb)
    layout = Row(button, button2)

    port = 6015
    serve(layout, port=port, threaded=True, show=False)

    # Wait for server to start
    time.sleep(1)

    requests.get(f"http://localhost:{port}/")

    model = list(layout._models.values())[0][0]
    doc = model.document
    with set_curdoc(doc):
        button._server_change(doc, model.ref['id'], None, 'clicks', 0, 1)
        button2._server_change(doc, model.ref['id'], None, 'clicks', 0, 1)

    # Wait for callbacks to be scheduled
    time.sleep(1)

    # Checks whether Button on_click callback was executed concurrently
    assert max(counts) == 2