Skip to main content

Wrapper for running/rendering OpenAI Gym on Jupyter Notebook

Project description

Gym-Notebook-Wrapper

PyPI - Python Version PyPI PyPI - Status PyPI - License

Gym-Notebook-Wrapper provides small wrappers for running and rendering OpenAI Gym on Jupyter Notebook or similar (e.g. Google Colab).

1. Requirement

  • Linux
  • Xvfb
    • On Ubuntu, you can install sudo apt update && sudo apt install xvfb.
  • Open GL (for some environment)
    • On Ubuntu, you can install sudo apt update && sudo apt install python-opengl

2. Installation

You can install from PyPI with pip install gym-notebook-wrapper

3. Usage

Three classes are implemented in gnwrapper module in this gym-notebook-wrapper package.

3.1 Simple One Shot Animation

Wrap gym.Env class with gnwrapper.Animation. That's all! The render() method shows the environment on its output. An example code is following;

3.1.1 Code

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()

3.1.2 Limitation

  • Calling render() method delete the other output for the same cell.
  • The output image is shown only once.

3.2 Loop Animation

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.

3.2.1 Code

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()

3.2.2 Limitation

  • Require a lot of memory to store and display large steps of display
    • Can raise memory error

3.3 Movie Animation

Wrap gym.Env class with gnwrapper.Monitor. This wrapper inherits gym.wrappers.Monitor (for gym<=0.19.0) or gym.wrapper.RecordVideo (for gym>=0.20.0), and implements display() method for embedding mp4 movie into Notebook.

Note: gym.wrappers.Monitor was deprecated at gym==0.20.0, which was released on 14th September 2021. See.

If you call display(reset=True), the video list is cleared and the next display() method shows only new videos.

3.3.1 Code

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()

3.3.2 Limitation

  • Require disk space for save movie

4. Notes

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).

5. Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gym_notebook_wrapper-1.2.5-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file gym_notebook_wrapper-1.2.5-py3-none-any.whl.

File metadata

  • Download URL: gym_notebook_wrapper-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for gym_notebook_wrapper-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c4ae4cacc97ad237ab3b6c1205437c60279155363fdce1f0bac9d749ffbcd386
MD5 06250d6e5af5f9e2f2ea94f87b95b75d
BLAKE2b-256 fdb878e2416253ce4a83d6e59b23e7ec2836fca0233fd708d300c269959b8296

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page