Ruby on Rails in Deepnote
The power of rails console
Joining the Deepnote development team after years in Ruby on Rails, the one thing I missed the most was the powerful rails console
.
Being able to connect to staging or production server, fire up rails c
and query data based on the existing ActiveRecord models, their scopes and relations had been extremely helpful in many situations.
Over time, I developed a number of "Rails console snippets" -- typically sequences of where
s, join
s, filter
s and map
s - and kept a text file with these stored somewhere on my computer.
rails console
+ Deepnote = ❤️
Together with Simon, we thought that having the power of rails console
combined with a Notebook experience could be a useful tool in the toolbox of a Rails developer. Adding the collaborative nature of Deepnote makes it even more powerful, as it gives you a central storage for all of those "Rails snippets text file"s that were spread around developers' computers.
Using different levels of sharing, the notebooks crafted by engineers can then also be shared with less technical colleagues who can either only have Read or Execute rights to fetch the data they need. Deepnote's support of Custom Environments has made this possible. In this article, we will show you how bring in Ruby on Rails into Deepnote.
Bringing your Ruby on Rails application to Deepnote
For the demo purposes, we have created a simple Rails application. It is basically just a skeleton generated by rails new
.
Dockerfile
We assume that you already have an existing Dockerfile
for your application. To be able to run your application in Deepnote, we need to create Docker image on top of your existing Docker image. The Dockerfile in our demo application uses Docker multi stage build, but you can simply create new Dockerfile and point the FROM
to an existing image with your application.
The Dockerfile needed for Deepnote should look like this:
FROM base
RUN apt-get update -qq && apt-get install -y python3 python3-pip python3-venv libtool libffi-dev make libzmq3-dev libczmq-dev
RUN gem install ffi-rzmq
RUN gem install iruby --pre
RUN ln -s $(which python3) /usr/local/bin/python && ln -s $(which pip3) /usr/local/bin/pip
ENV DEFAULT_KERNEL_NAME=ruby
We are hosting the Docker image for the demo application publicly at DockerHub and I have configured my Deepnote project to use this custom Docker image. This means the application together with the libraries will be available right at the project boot -- no need to wait for bundle install
when starting the project.
You can also host your Docker image in private DockerHub repository, private AWS ECR or private GCR. The private repositories are only available with the Deepnote Team subscription.
init.ipynb
For the Ruby on Rails to work in Deepnote, the following has to be added to the init.ipynb
initialization notebook
!cd /myapp && iruby register --force
Database
The easiest way to set up the database connection is to by using the Environment Variables integration. We will set the DATABASE_URL
environment variable to the desired connection string there.
I use GCP hosted PostgreSQL database for the demo, but any database supported by your application will work. Using a read-only replica for this purpose could be a good idea.
Notebook
With all the setup done, you can now finally start writing Ruby in a new notebook. There is one last step -- you need to include the following cell to your notebook:
Models
Once you are done with this 🎉, you can finally start writing your code. You are most likely going to start by querying some of your models to verify that your database connection works.
You can use the scopes
on the models as well and the code completion will even help you with that
Dataframes
You can use the rover-df gem to view data as table. To use this, you need to list rover-df
in your Gemfile
.
Visualization
Similarly, you can use vega.rb for graphs:
Disclaimer
Despite all our love for Ruby on Rails, Deepnote is primarily designed for data science workflows in Python and R. While can get your Ruby on Rails app working in Deepnote, some features (e.g., the variable explorer and SQL cells) will not work at this stage.
Get in touch
We covered the basics of getting your Ruby-on-Rails app context available in a notebook environment in Deepnote. We believe this has the potential to bring lots of collaborative power to your team. If you have any questions about how to set this up, reach out to us at help@deepnote.com! We're more than happy to help.