How do I make a Jupylet app working inside of an if statement in a Jupyter notebook

Viewed 86

I am using a Jupylet app inside of my notebook. If I run app.run() inside of a cell it works fine, but if I trigger the run from an if statement as in my example code, it does nothing. There are no errors, but the app does not show up. How can I solve this?

    import sys
    import os
    sys.path.insert(0, os.path.abspath('./..'))
    from jupylet.label import Label
    from jupylet.app import App
    
    app = App(width=320, height=64)
    hello = Label('hello, world', color='cyan', font_size=32, x=app.width, y=22)
    
    @app.run_me_every(1/24)
    def scroll(ct, dt):
        hello.x -= dt * 48
        if hello.right < 0:
            hello.x = app.width
            
    @app.event
    def render(ct, dt):
        app.window.clear()
        hello.draw() 
    
    c = 3
    if c == 3:
        app.run()
1 Answers

In Jupylet, app.run() returns a Jupyter ipywidgets.Image object. it is that object that gets displayed when you run the cell.

You can access it with app.canvas after the call to app.run() and if you evaluate it in a cell the game canvas should be displayed. For example:

In []: app.canvas
Out[]: ...game canvas should display here...
Related