Gym-Notebook-Wrapper provides small wrappers for running and rendering OpenAI Gym on Jupyter Notebook or similar (e.g. Google Colab).
- Linux
- Xvfb
- On Ubuntu, you can install
sudo apt update && sudo apt install xvfb.
- On Ubuntu, you can install
- Open GL (for some environment)
- On Ubuntu, you can install
sudo apt update && sudo apt install python-opengl
- On Ubuntu, you can install
You can install from
PyPI with pip install gym-notebook-wrapper
Three classes are implemented in gnwrapper module in this
gym-notebook-wrapper package.
Wrap gym.Env class with gnwrapper.Animation. That's all! The
render() method shows the environment on its output. An example code
is following;
import gnwrapper
import gym
env = gnwrapper.Animation(gym.make('CartPole-v1'))
obs = env.reset()
for _ in range(1000):
next_obs, reward, done, info = env.step(env.action_space.sample())
env.render()
obs = next_obs
if done:
obs = env.reset()- Calling
render()method delete the other output for the same cell. - The output image is shown only once.
Wrap gym.Env class with gnwrapper.LoopAnimation. This wrapper
stores display image when render() methos is called and shows the
loop animation by display(dpi=72,interval=50) methos.
import gnwrapper
import gym
env = gnwrapper.LoopAnimation(gym.make('CartPole-v1'))
obs = env.reset()
for _ in range(100):
next_obs, reward, done, info = env.step(env.action_space.sample())
env.render()
obs = next_obs
if done:
obs = env.reset()
env.display()- Require a lot of memory to store and display large steps of display
- Can raise memory error
Wrap gum.Env class with gnwrapper.Monitor. This wrapper inherits
gym.wrappers.Monitor and implements display() method for embedding
mp4 movie into Notebook.
If you call display(reset=True), the video list is cleared and the
next display() method shows only new videos.
import gnwrapper
import gym
env = gnwrapper.Monitor(gym.make('CartPole-v1'),directory="./")
o = env.reset()
for _ in range(100):
o, r, d, i = env.step(env.action_space.sample())
if d:
env.reset()
env.display()- Require disk space for save movie
gnwrapper.Animation and gnwrapper.LoopAnimation inherit from
gym.Wrapper, so that it can access any fields or mothods of
gym.Env and gym.Wrapper (e.g. action_space).