Esempio n. 1
0
def test_invalid_event_name():
    # An unrecognized event name should generate a ValueError
    event_widget = Event()
    bad_name = 'this is not a valid event name'
    with pytest.raises(ValueError) as e:
        event_widget.watched_events = [bad_name]

    assert bad_name in str(e)
    assert 'not supported. The supported ' in str(e)
Esempio n. 2
0
def test_valid_event_name():
    # A valid event name should result in that attribute being set.
    event_widget = Event()
    event_widget.watched_events = ['click']
    assert ['click'] == event_widget.watched_events
Esempio n. 3
0
def run():
    # Time to build the figure!

    fig, ax = plt.subplots(figsize=[9, 9])

    # Draw the stars.

    def scatter_stars():
        marker_size = (0.5 + limiting_magnitude - stars['magnitude']) ** 2.0

        mask = (
            (marker_size > 0.25)
            & (stars['x'] > -0.3)
            & (stars['x'] < 0.3)
            & (stars['y'] > -0.3)
            & (stars['y'] < 0.3)
        )

        #print('Number of stars:', mask.sum())

        #scatter.set_data(stars['x'][mask], stars['y'][mask])
        xy = np.array([stars['x'][mask], stars['y'][mask]])
        scatter.set_offsets(xy.T)

        s = marker_size[mask] #[bright_stars]]
        scatter.set_sizes(s)

    scatter = ax.scatter([], [], color='k')
    scatter_stars()

    # Finally, title the plot and set some final parameters.

    angle = np.pi - field_of_view_degrees / 360.0 * np.pi
    limit = np.sin(angle) / (1.0 - np.cos(angle))
    #print(limit)
    ax.set_xlim(-limit, limit)
    ax.set_ylim(-limit, limit)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.set_aspect(1.0)
    ax.set_title('Stars')

    from ipywidgets import Label, HTML, HBox, Image, VBox, Box, HBox, interact
    from ipyevents import Event
    from IPython.display import display

    l = Label('Click or type on me!')
    l.layout.border = '2px solid red'

    h = HTML('Event info')
    d = Event(source=l, watched_events=['click', 'keydown', 'mouseenter'])

    def handle_event(event):
        lines = ['{}: {}'.format(k, v) for k, v in event.items()]
        content = '<br>'.join(lines)
        h.value = content

    d.on_dom_event(handle_event)

    #display(l, h)

    import ipywidgets as widgets

    def demo(i):
        field_of_view_degrees = 45.0 - i
        angle = np.pi - field_of_view_degrees / 360.0 * np.pi
        limit = np.sin(angle) / (1.0 - np.cos(angle))

        ax.set_xlim(-limit, limit)
        ax.set_ylim(-limit, limit)
        return fig

    #widgets.interact(demo, i = d)

    def callback(event):
        try:
            return callback2(event)
        except Exception as e:
            place.value = str(e)

    def callback2(event):
        nonlocal limit
        place.value = str(event['code'])
        code = event['code']
        if event['code'] == 'PageUp':
            limit /= 1.1
            ax.set_xlim(-limit, limit)
            ax.set_ylim(-limit, limit)
        elif event['code'] == 'PageDown':
            limit *= 1.1
            ax.set_xlim(-limit, limit)
            ax.set_ylim(-limit, limit)
        elif code.startswith('Arrow'):
            if code == 'ArrowUp':
                set_center(0, 0.5)
            elif code == 'ArrowDown':
                set_center(0, -0.5)
            elif code == 'ArrowLeft':
                set_center(0.1, 0)
            elif code == 'ArrowRight':
                set_center(-0.1, 0)
            set_positions()
            scatter_stars()
            place.value = 'Surv'

        #PageUp: bigger
        #    PageDown: smaller
        redraw()

    from io import BytesIO

    def redraw():
        io = BytesIO()
        plt.savefig(io, format="png")
        #plt.close()
        plt.ioff()
        image.value = io.getvalue()

    image = Image(format='png')
    redraw()

    # The layout bits below make sure the image display looks the same in lab and classic notebook 
    image.layout.max_width = '4in'
    image.layout.height = 'auto'

    im_events = Event()
    im_events.source = image
    im_events.watched_events = ['keydown']
    im_events.on_dom_event(callback)

    place = HTML('Test')
    return VBox([place, image])