Skip to content

Instantly share code, notes, and snippets.

@cfperez
Forked from andrewschreiber/jupyter_gym_render.md
Created August 30, 2018 17:45
Show Gist options
  • Save cfperez/eaa561573397e61a979972b3bc784f75 to your computer and use it in GitHub Desktop.
Save cfperez/eaa561573397e61a979972b3bc784f75 to your computer and use it in GitHub Desktop.
How to stream OpenAI Gym environment rendering within a Jupyter Notebook

Open jupyter with

$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

In Jupyter

import matplotlib.pyplot as plt
%matplotlib inline

After each step

def show_state(env, step=0):
    plt.figure(3)
    plt.clf()
    plt.imshow(env.render(mode='rgb_array'))
    plt.title("%s. Step: %d" % (env._spec.id,step))
    
    plt.pause(0.001)  # pause for plots to update

    if is_ipython:
        display.clear_output(wait=True)
        display.display(plt.gcf())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment