Jupyter is a tool for running interactive notebooks; basically add Python with Markdown and you've got Jupyter. if you haven't used it before, I recommend you do.
In this post, I'm going to show you how to deploy a Jupyter Notebook server on Heroku using Docker.
Jupyter has the ability to create new notebooks and they will 100% save on your deployed docker-based Jupyter server... but they will disappear as soon as you deploy a new version. That's because containers, by their very nature, are ephemeral by default.
This caveat doesn't mean we shouldn't do this... it just means it is a HUGE consideration when using this guide over something like http://colab.research.google.com.
Near the bottom, I'll show you how to package all your Jupyter contents, download it, and unpackage it again when you deploy.
cfe_jupyter
| Dockerfile
│ Pipfile
│ Pipfile.lock
│
└───conf
│ │ jupyter.py
|
└───nbs
│ │ notebook.tar.gz
│
└───scripts
│ Dockerfile
│ d_build.sh
| d_run.sh
| deploy.sh
| entrypoint.sh
pipenv
and install jupyter
pip install pipenv
cd path/to/your/project/
pipenv install jupyter --python 3.8
Generate Default Config
jupyter notebook --generate-config
This command creates the default jupyter_notebook_config.py
file on your local machine. Mine was stored on ~/.jupyter/jupyter_notebook_config.py
Create conf/jupyter.py
mkdir conf
echo "" > conf/jupyter.py
In conf/jupyter.py
add:
```python import os c = get_config()
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook
c.NotebookApp.notebook_dir = 'nbs' c.NotebookApp.allow_origin = u'cfe-jupyter.herokuapp.com' # put your public IP Address here c.NotebookApp.ip = '*' c.NotebookApp.allow_remote_access = True c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:8da45965a489:86884d5b174e2f64e900edd129b5ef0d2f784a65' c.NotebookApp.port = int(os.environ.get("PORT", 8888)) c.NotebookApp.allow_root = True c.NotebookApp.allow_password_change = True c.ConfigurableHTTPProxy.command = ['configurable-http-proxy', '--redirect-port', '80'] ``` A few noteable setup items here:
c.NotebookApp.notebook_dir
I set as nbs
which means you should create a directory as nbs
for your default notebooks directory. In my case, jupyter will open right to this directory ignoring all others.c.NotebookApp.password
- this has to be a hashed password. To create a new one, just run ipython -c "from notebook.auth import passwd; passwd()"
on your command line.c.NotebookApp.port
- Heroku sets this value in our environment variables thus int(os.environ.get("PORT", 8888))
as our default.Test your new configuration locally with: jupyter notebook --config=./conf/jupyter.py
nbs/Load_Unload.ipynb
This will be how you can handle the ephemeral nature of Docker containers with Jupyter notebooks. Just create a new notebook called Load_Unload.ipynb
, and add the following:
```python mode = "unload"
if mode == 'unload': # Zip all files in the current directory !tar chvfz notebook.tar.gz *
elif mode == 'load: # Unzip all files in the current directory !!tar -xv -f notebook.tar.gz ```
Dockerfile
This is the absolute minimum setup here. You might want to add additional items as needed. Certain packages, especially the ones for data science, require additional installs for our docker-based linux server.
```dockerfile FROM python:3.8.2-slim
ENV APP_HOME /app WORKDIR ${APP_HOME}
COPY . ./
RUN pip install pip pipenv --upgrade RUN pipenv install --skip-lock --system --dev
CMD ["./scripts/entrypoint.sh"] ```
The most noteable part of this all is that (1) I'm using
pipenv
locally and in docker and (2) I both installpipenv
and runpipenv install --system
to install all pipenv dependancies to the entire docker container (instead of in a virtual environment within the container as well).
scripts/entrypoint.sh
I perfer using a entrypoint.sh
script for the CMD
in Dockerfiles.
```bash
/usr/local/bin/jupyter notebook --config=./conf/jupyter.py ```
``` docker build -t cfe-jupyter -f Dockerfile .
docker run --env PORT=8888 -it -p 8888:8888 cfe-jupyter ```
heroku create cfe-jupyter
- Change cfe-jupyter
to your app name
``` heroku container:login
```
bash
heroku container:push web
heroku container:release web
web
is the default for our Dockerfile
. -a <your-app-name>
like `heroku container:push web -a cfe-jupyter heroku open
This should allow you to open up your project.
Dockerfile
```dockerfile FROM python:3.8.2-slim
ENV APP_HOME /app WORKDIR ${APP_HOME}
COPY . ./
RUN pip install pip pipenv --upgrade RUN pipenv install --skip-lock --system --dev
CMD ["./scripts/entrypoint.sh"] ```
Pipfile
``` [[source]] name = "pypi" url = "https://pypi.org/simple" verify_ssl = true
[dev-packages]
[packages] jupyter = "*"
[requires] python_version = "3.8" ```
scripts/d_build.sh
bash
docker build -t cfe-jupyter -f Dockerfile .
scripts/d_run.sh
bash
docker run --env PORT=8888 -it -p 8888:8888 cfe-jupyter
scripts/deploy.sh
bash
heroku container:push web
heroku container:release web
scripts/entrypoint.sh
```bash
/usr/local/bin/jupyter notebook --config=./conf/jupyter.py ```
conf/jupyter.py
```python import os c = get_config()
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook
c.NotebookApp.notebook_dir = 'nbs' c.NotebookApp.allow_origin = u'cfe-jupyter.herokuapp.com' # put your public IP Address here c.NotebookApp.ip = '*' c.NotebookApp.allow_remote_access = True c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:8da45965a489:86884d5b174e2f64e900edd129b5ef0d2f784a65' c.NotebookApp.port = int(os.environ.get("PORT", 8888)) c.NotebookApp.allow_root = True c.NotebookApp.allow_password_change = True c.ConfigurableHTTPProxy.command = ['configurable-http-proxy', '--redirect-port', '80'] ```
nbs/Load_Unload.ipynb
```python mode = "unload"
if mode == 'unload': # Zip all files in the current directory !tar chvfz notebook.tar.gz *
elif mode == 'load: # Unzip all files in the current directory !!tar -xv -f notebook.tar.gz ```
You might need additional packages (like numpy
or pandas
or opencv
) in your project. Here's what you need to do in your Dockerfile
, (on our repo the final docker file is listed as Dockerfile.Bonus
) just update it to the following:
```dockerfile FROM python:3.8.2-slim
ENV APP_HOME /app WORKDIR ${APP_HOME}
COPY . ./
RUN apt-get update && apt-get install -y --no-install-recommends \ tzdata \ libopencv-dev \ build-essential \ libssl-dev \ libpq-dev \ libcurl4-gnutls-dev \ libexpat1-dev \ gettext \ unzip \ supervisor \ python3-setuptools \ python3-pip \ python3-dev \ python3-venv \ python3-urllib3 \ git \ && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*
RUN pip install pip pipenv --upgrade
RUN pip install scikit-learn opencv-contrib-python numpy pandas
RUN pip install tensorflow keras
RUN apt-get update && apt-get -y install gcc mono-mcs && rm -rf /var/lib/apt/lists/* RUN pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install fastai
RUN pipenv install --skip-lock --system --dev
CMD ["./scripts/entrypoint.sh"] ```
Bumps nbconvert from 5.6.1 to 6.5.1.
Sourced from nbconvert's releases.
Release 6.5.1
No release notes provided.
6.5.0
What's Changed
- Drop dependency on testpath. by
@anntzer
in jupyter/nbconvert#1723- Adopt pre-commit by
@blink1073
in jupyter/nbconvert#1744- Add pytest settings and handle warnings by
@blink1073
in jupyter/nbconvert#1745- Apply Autoformatters by
@blink1073
in jupyter/nbconvert#1746- Add git-blame-ignore-revs by
@blink1073
in jupyter/nbconvert#1748- Update flake8 config by
@blink1073
in jupyter/nbconvert#1749- support bleach 5, add packaging and tinycss2 dependencies by
@bollwyvl
in jupyter/nbconvert#1755- [pre-commit.ci] pre-commit autoupdate by
@pre-commit-ci
in jupyter/nbconvert#1752- update cli example by
@leahecole
in jupyter/nbconvert#1753- Clean up pre-commit by
@blink1073
in jupyter/nbconvert#1757- Clean up workflows by
@blink1073
in jupyter/nbconvert#1750New Contributors
@pre-commit-ci
made their first contribution in jupyter/nbconvert#1752Full Changelog: https://github.com/jupyter/nbconvert/compare/6.4.5...6.5
6.4.3
What's Changed
- Add section to
customizing
showing how to use template inheritance by@stefanv
in jupyter/nbconvert#1719- Remove ipython genutils by
@rgs258
in jupyter/nbconvert#1727- Update changelog for 6.4.3 by
@blink1073
in jupyter/nbconvert#1728New Contributors
@stefanv
made their first contribution in jupyter/nbconvert#1719@rgs258
made their first contribution in jupyter/nbconvert#1727Full Changelog: https://github.com/jupyter/nbconvert/compare/6.4.2...6.4.3
6.4.0
What's Changed
- Optionally speed up validation by
@gwincr11
in jupyter/nbconvert#1672- Adding missing div compared to JupyterLab DOM structure by
@SylvainCorlay
in jupyter/nbconvert#1678- Allow passing extra args to code highlighter by
@yuvipanda
in jupyter/nbconvert#1683- Prevent page breaks in outputs when printing by
@SylvainCorlay
in jupyter/nbconvert#1679- Add collapsers to template by
@SylvainCorlay
in jupyter/nbconvert#1689- Fix recent pandoc latex tables by adding calc and array (#1536, #1566) by
@cgevans
in jupyter/nbconvert#1686- Add an invalid notebook error by
@gwincr11
in jupyter/nbconvert#1675- Fix typos in execute.py by
@TylerAnderson22
in jupyter/nbconvert#1692- Modernize latex greek math handling (partially fixes #1673) by
@cgevans
in jupyter/nbconvert#1687- Fix use of deprecated API and update test matrix by
@blink1073
in jupyter/nbconvert#1696- Update nbconvert_library.ipynb by
@letterphile
in jupyter/nbconvert#1695- Changelog for 6.4 by
@blink1073
in jupyter/nbconvert#1697New Contributors
... (truncated)
7471b75
Release 6.5.1c1943e0
Fix pre-commit8685e93
Fix tests0abf290
Run black and prettier418d545
Run test on 6.x branchbef65d7
Convert input to string prior to escape HTML0818628
Check input type before escapingb206470
GHSL-2021-1017, GHSL-2021-1020, GHSL-2021-1021a03cbb8
GHSL-2021-1026, GHSL-2021-102548fe71e
GHSL-2021-1024Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps mistune from 0.8.4 to 2.0.3.
Sourced from mistune's releases.
Version 2.0.2
Fix
escape_url
via lepture/mistune#295Version 2.0.1
Fix XSS for image link syntax.
Version 2.0.0
First release of Mistune v2.
Version 2.0.0 RC1
In this release, we have a Security Fix for harmful links.
Version 2.0.0 Alpha 1
This is the first release of v2. An alpha version for users to have a preview of the new mistune.
Sourced from mistune's changelog.
Changelog
Here is the full history of mistune v2.
Version 2.0.4
Released on Jul 15, 2022
- Fix
url
plugin in<a>
tag- Fix
*
formattingVersion 2.0.3
Released on Jun 27, 2022
- Fix
table
plugin- Security fix for CVE-2022-34749
Version 2.0.2
Released on Jan 14, 2022
Fix
escape_url
Version 2.0.1
Released on Dec 30, 2021
XSS fix for image link syntax.
Version 2.0.0
Released on Dec 5, 2021
This is the first non-alpha release of mistune v2.
Version 2.0.0rc1
Released on Feb 16, 2021
Version 2.0.0a6
</tr></table>
... (truncated)
3f422f1
Version bump 2.0.3a6d4321
Fix asteris emphasis regex CVE-2022-347495638e46
Merge pull request #307 from jieter/patch-10eba471
Fix typo in guide.rst61e9337
Fix table plugin76dec68
Add documentation for renderer heading when TOC enabled799cd11
Version bump 2.0.2babb0cf
Merge pull request #295 from dairiki/bug.escape_urlfc2cd53
Make mistune.util.escape_url less aggressive3e8d352
Version bump 2.0.1Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps notebook from 6.0.3 to 6.4.12.
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps ipython from 7.16.1 to 7.16.3.
d43c7c7
release 7.16.35fa1e40
Merge pull request from GHSA-pq7m-3gw7-gq5x8df8971
back to dev9f477b7
release 7.16.2138f266
bring back release helper from master branch5aa3634
Merge pull request #13341 from meeseeksmachine/auto-backport-of-pr-13335-on-7...bcae8e0
Backport PR #13335: What's new 7.16.28fcdcd3
Pin Jedi to <0.17.2.Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps pygments from 2.6.1 to 2.7.4.
Sourced from pygments's releases.
2.7.4
Updated lexers:
Fix infinite loop in SML lexer (#1625)
Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)
Limit recursion with nesting Ruby heredocs (#1638)
Fix a few inefficient regexes for guessing lexers
Fix the raw token lexer handling of Unicode (#1616)
Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!
Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)
Fix incorrect MATLAB example (#1582)
Thanks to Google's OSS-Fuzz project for finding many of these bugs.
2.7.3
... (truncated)
Sourced from pygments's changelog.
Version 2.7.4
(released January 12, 2021)
Updated lexers:
Fix infinite loop in SML lexer (#1625)
Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)
Limit recursion with nesting Ruby heredocs (#1638)
Fix a few inefficient regexes for guessing lexers
Fix the raw token lexer handling of Unicode (#1616)
Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!
Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)
Fix incorrect MATLAB example (#1582)
Thanks to Google's OSS-Fuzz project for finding many of these bugs.
Version 2.7.3
(released December 6, 2020)
... (truncated)
4d555d0
Bump version to 2.7.4.fc3b05d
Update CHANGES.ad21935
Revert "Added dracula theme style (#1636)"e411506
Prepare for 2.7.4 release.275e34d
doc: remove Perl 6 ref2e7e8c4
Fix several exponential/cubic complexity regexes found by Ben Caller/Doyenseceb39c43
xquery: fix pop from empty stack2738778
fix coding style in test_analyzer_lexer02e0f09
Added 'ERROR STOP' to fortran.py keywords. (#1665)c83fe48
support added for css variables (#1633)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Build real projects and learn to code. Step by step. By @jmitchel3
GitHub Repository Homepagejupyter python notebooks server heroku docker tutorial