The deployment docs are split up in two parts. First there is the general section, which should always be followed. Then there is a cloud service provider specific section of which one provider should be chosen.
If you are not sure where to deploy your app, we suggest Binder or Heroku. You can test deploying and serving your app without having to enter any credit card details, and with very little prior experience of deployments.
Create a project directory of notebooks you wish to display. For this tutorial we will clone Voilà and treat the notebooks folder as our project root.
git clone git@github.com:voila-dashboards/voila.git cd voila/notebooks/
Add a requirements.txt file to the project directory. This file should contain all the Python dependencies your Voilà app needs to run. For this tutorial we will copy the contents of the environment.yml of Voilà. We omit xleaflet and xeus-cling because these require extra work that is beyond the scope of this guide.
bqplot ipympl ipyvolume scipy voila
Binder is one of the most accessible ways to deploy Voilà applications. The service is available at mybinder.org and is increasingly being used for reproducible research, making it an excellent fit for deploying Voilà applications.
- Make sure the repository is publicly available (on GitHub, Gitlab or as a gist).
- Follow this guide to prepare the repository. For simple deployments, steps listed in Setup an example project will be sufficient.
Note
Binder also supports environment.yml
files and conda
environments.
- Go to mybinder.org and enter the URL of the repository.
- In
Path to a notebook file
, selectURL
and use the Voilà endpoint:/voila/render/path/to/notebook.ipynb
- Click
Launch
. - Binder will trigger a new build if this is the first launch (or if there has been new changes since the last build). This might take a few minutes to complete. If an image is already available, the server will be able to start within a few seconds.
To specify different options (such as the theme and template), create a jupyter_config.json
file at the root of the repository with the following content:
{ "VoilaConfiguration": { "theme": "dark", "template": "gridstack" } }
An example can be found in the voila-demo repository.
Heroku.com is an attractive option if you want to try out deployment for free. You have limited computing hours, however the app will also automatically shutdown if it is idle.
The general steps for deployment at Heroku can be found here. High level instructions, specific to voila can be found below:
- Follow the steps of the official documentation to install the heroku cli and login on your machine.
Add a file named runtime.txt to the project directory with the following content:
python-3.7.3
Add a file named Procfile to the project directory with the following content if you want to show all notebooks:
web: voila —-port=$PORT --no-browser
Or the following if you only want to show one notebook:
web: voila —-port=$PORT —-no-browser your_notebook.ipynb
Initialize your git repo and commit your code. At minimum you need to commit your notebooks, requirements.txt, runtime.txt, and the Procfile.
git init git add <your-files> git commit -m "my message"
Create an Heroku instance and push the code.
heroku create git push heroku master
Open your web app
heroku open
To resolve issues, it is useful to see the logs of your application. You can do this by running:
heroku logs --tail
You can deploy on Google App Engine in a “flexible” environment. This means that the underlying machine will always run. This is more expensive than a “standard” environment, which is similar to Heroku’s free option. However, Google App Engine’s “standard” environment does not support websockets, which is a requirement for voila.
The general steps for deployment at Google App Engine can be found here. High level instructions specific to Voilà can be found below:
- Follow the “Before you begin steps” from the official documentation to create your account, project and App Engine app.
Add an app.yaml file to the project directory with the following content:
runtime: python env: flex runtime_config: python_version: 3 entrypoint: voila --port=$PORT --no-browser
Edit the last line if you want to show only one notebook
entrypoint: voila --port=$PORT --no-browser your_notebook.ipynb
Deploy your app
gcloud app deploy
Open your app
gcloud app browse
- A server running Ubuntu 18.04 (or later) with root access.
- Ability to SSH into the server and run commands from the prompt.
- The public IP address of the server.
- A domain name pointing to the IP address of the server.
SSH into the server:
ssh ubuntu@<ip-address>
Install nginx:
sudo apt install nginx
To check that
nginx
is correctly installed:sudo systemctl status nginx
Create the file
/etc/nginx/sites-enabled/yourdomain.com
with the following content:server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:8866; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; } client_max_body_size 100M; error_log /var/log/nginx/error.log; }
Enable and start the
nginx
service:sudo systemctl enable nginx.service sudo systemctl start nginx.service
Install pip:
sudo apt update && sudo apt install python3-pip
Follow the instructions in Setup an example project, and install the dependencies:
sudo python3 -m pip install -r requirements.txt
8. Create a new systemd service for running voila in /usr/lib/systemd/system/voila.service
. The service will ensure voila is automatically restarted on startup:
[Unit] Description=Voila [Service] Type=simple PIDFile=/run/voila.pid ExecStart=voila --no-browser voila/notebooks/basics.ipynb User=ubuntu WorkingDirectory=/home/ubuntu/ Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
In this example voila is started with voila --no-browser voila/notebooks/basics.ipynb
to serve a single notebook. You can edit the command to change this behavior and the notebooks voila is serving.
Enable and start the
voila
service:sudo systemctl enable voila.service sudo systemctl start voila.service
Note
To check the logs for voila:
journalctl -u voila.service
- Now go to
yourdomain.com
to access the voila application.
Install
certbot
:sudo add-apt-repository ppa:certbot/certbot sudo apt update sudo apt install python-certbot-nginx
Obtain the certificates from Let's Encrypt. The
--nginx
flag will edit the nginx configuration automatically:sudo certbot --nginx -d yourdomain.com
/etc/nginx/sites-enabled/yourdomain.com
should now contain a few more entries:- Visit https://yourdomain.com to access the voila applications over HTTPS.
To automatically renew the certificates (they expire after 90 days), open the
crontab
file:
And add the following line:
For more information, you can also follow the guide on the nginx blog.
ngrok is a useful tool to expose local servers to the public internet over secure tunnels. It can be used to share Voilà applications served by a local instance of Voilà.
The main use case for using Voilà with ngrok is to quickly share a notebook as an interactive application with
Warning
Don't forget to exercise caution before exposing local apps and data to the public over the internet.
While Voilà does not permit arbitrary code execution, be aware that sensitive information could be exposed, depending on the content and the logic of the notebook.
It's good practice to keep the ngrok tunnel connection short-lived, and limit its use to quick sharing purposes.
To setup ngrok, follow the Download and setup ngrok guide.
- Start Voilà locally:
voila --no-browser my_notebook.ipynb
- In a new terminal window, start ngrok:
ngrok http 8866
- Copy the link from the ngrok terminal window. The links looks like the following: https://8bb6fded.ngrok.io/
- Send the link
- When using the ngrok link, the requests will be forwared to your local instance of Voilà.