To embed an iPython Notebook (now known as Jupyter Notebook) in a Rails application, you can use the nbviewer service or the jupyter-rails gem. Both methods allow you to display Jupyter Notebooks within your Rails application.

Method 1: Using nbviewer

The nbviewer service allows you to display Jupyter Notebooks by simply passing the notebook URL to the nbviewer service, which will render the notebook as an HTML page.

  1. Open the Jupyter Notebook you want to embed.
  2. Click on the "Share" button in the notebook toolbar and copy the notebook URL.
  3. In your Rails application view, use an iframe to embed the notebook using the nbviewer service:
<iframe src="" width="100%" height="600"></iframe>

Replace YOUR_NOTEBOOK_URL with the URL of your Jupyter Notebook.

Method 2: Using jupyter-rails Gem

The jupyter-rails gem allows you to run Jupyter Notebooks within your Rails application as a local service. This method provides more control over the display and interaction with the notebook, as it runs the notebook as part of your Rails application.

  1. Install the jupyter-rails gem:
# Gemfile gem 'jupyter-rails'
  1. Run bundle install:
bundle install
  1. Generate the necessary files:
rails generate jupyter:install
  1. Start the Jupyter Notebook server:
rails jupyter:start
  1. In your Rails application view, use an iframe to display the notebook:
<iframe src="http://localhost:8888" width="100%" height="600"></iframe>

Adjust the iframe width and height as needed.

With this setup, you can interact with the Jupyter Notebook within your Rails application and use the full functionality of Jupyter Notebooks.

Both methods have their advantages. Using nbviewer is simple and requires no setup, but you have less control over the display and functionality. On the other hand, using the jupyter-rails gem provides more control and interactivity but requires additional setup and maintenance. Choose the method that best suits your needs and development environment.

Have questions or queries?
Get in Touch