Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Last active October 30, 2022 08:18
Show Gist options
  • Save terasakisatoshi/911efa1b8ce8a2e83dde0f271a26db74 to your computer and use it in GitHub Desktop.
Save terasakisatoshi/911efa1b8ce8a2e83dde0f271a26db74 to your computer and use it in GitHub Desktop.
Reduce overhead of loading Plots.jl packages on jupyter using PackageCompiler.jl ( tested on Julia v1.5.0 )
FROM julia:1.5.1
RUN apt-get update && \
apt-get install -y \
build-essential \
cmake \
python3 \
python3-dev \
python3-distutils \
curl \
ca-certificates \
git \
libgconf-2-4 \
xvfb \
libgtk-3-0 \
dvipng \
texlive-latex-recommended \
zip \
libxt6 libxrender1 libxext6 libgl1-mesa-glx libqt5widgets5 # GR && \
apt-get clean && rm -rf /var/lib/apt/lists/* # clean up
RUN curl -kL https://bootstrap.pypa.io/get-pip.py | python3 && \
pip3 install \
jupyter \
jupyterlab \
jupytext \
ipywidgets \
jupyter-contrib-nbextensions \
jupyter-nbextensions-configurator \
jupyterlab_code_formatter autopep8 black
# suppress warning for related to GR backend
ENV GKSwstype=100
RUN julia -e 'ENV["PYTHON"]=Sys.which("python3"); \
ENV["JUPYTER"]=Sys.which("jupyter"); \
using Pkg; \
Pkg.add(["IJulia", "Plots", "PackageCompiler"]); \
# Install test dependencies for IJulia \
Pkg.add(PackageSpec(name="JSON", version="0.21.0")); \
# Install test dependencies for Plots \
Pkg.add(PackageSpec(name="Plots", version="1.6.0")); \
Pkg.add(PackageSpec(name="ImageMagick", version="1.1.6")); \
Pkg.add(PackageSpec(name="VisualRegressionTests", version="1.0.0")); \
Pkg.add(PackageSpec(name="FileIO", version="1.4.2")); \
Pkg.add(PackageSpec(name="StableRNGs", version="0.1.1")); \
Pkg.add(PackageSpec(name="Gtk", version="1.1.4")); \
Pkg.add(PackageSpec(name="GeometryTypes", version="0.8.3")); \
Pkg.add(PackageSpec(name="GeometryBasics", version="0.2.15")); \
Pkg.add(PackageSpec(name="HDF5", version="0.13.5")); \
Pkg.add(PackageSpec(name="PGFPlotsX", version="1.2.9")); \
Pkg.add(PackageSpec(name="StaticArrays", version="0.12.4")); \
Pkg.add(PackageSpec(name="OffsetArrays", version="1.1.3")); \
Pkg.add(PackageSpec(name="UnicodePlots", version="1.1.0")); \
Pkg.add(PackageSpec(name="Distributions", version="0.23.10")); \
Pkg.pin([\
"Plots", "ImageMagick", "VisualRegressionTests", "FileIO", \
"StableRNGs", "Gtk", "GeometryTypes", "GeometryBasics", \
"HDF5", "PGFPlotsX", "StaticArrays", "OffsetArrays", \
"UnicodePlots", "Distributions" \
])'
# show current state
RUN julia -e "using Pkg; Pkg.status()"
# generate precompile_statements_file
RUN xvfb-run julia \
--trace-compile=ijuliacompile.jl \
-e 'ENV["CI"]="true"; using Plots, IJulia; \
include(joinpath(pkgdir(IJulia), "test", "runtests.jl")); \
try include(joinpath(pkgdir(Plots), "test", "runtests.jl")) catch end'
# update sysimage
RUN julia -e 'using PackageCompiler; \
create_sysimage(\
[:IJulia, :Plots], \
precompile_statements_file="ijuliacompile.jl", \
replace_default=true\
)'
RUN julia -e 'using Pkg; Pkg.precompile(); using InteractiveUtils; versioninfo()'
CMD ["julia"]
@terasakisatoshi
Copy link
Author

terasakisatoshi commented Aug 7, 2020

Usage

  • Step0: Install Docker
  • Step1: Donwload the Dockerfile above
  • Step2: Run the following command
$ docker build -t plotfast -f Dockerfile .
$ docker run --rm -v $PWD:/work -w /work -p 8888:8888 plotfast jupyter notebook --notebook-dir /work --ip=0.0.0.0 --allow-root
[I 10:24:54.590 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[I 10:24:54.803 NotebookApp] JupyterLab extension loaded from /usr/local/lib/python3.7/dist-packages/jupyterlab
[I 10:24:54.803 NotebookApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[I 10:24:54.839 NotebookApp] [Jupytext Server Extension] Deriving a JupytextContentsManager from LargeFileManager
[I 10:24:54.841 NotebookApp] Serving notebooks from local directory: /work
[I 10:24:54.841 NotebookApp] Jupyter Notebook 6.1.1 is running at:
[I 10:24:54.841 NotebookApp] http://bdc90935cc6f:8888/?token=e48611799f8b841f074ae0e1ded05cc27cd345247a8d3262
[I 10:24:54.841 NotebookApp]  or http://127.0.0.1:8888/?token=e48611799f8b841f074ae0e1ded05cc27cd345247a8d3262
[I 10:24:54.841 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 10:24:54.844 NotebookApp] No web browser found: could not locate runnable browser.
[C 10:24:54.844 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/nbserver-1-open.html
    Or copy and paste one of these URLs:
        http://bdc90935cc6f:8888/?token=xxxxxxxxxx
     or http://127.0.0.1:8888/?token=xxxxxxxxxx
  • Step3: Open http://127.0.0.1:8888/?token=xxxxxxxxxx on your web browser (where token xxxxxxxxxx is depends on your environment)

@terasakisatoshi
Copy link
Author

Result

image

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Aug 9, 2020

For those who like to run locally with julia --trace-compile=traced.jl

ENV["GKSwstype"]=100
ENV["PYTHON"]=Sys.which("python3"); 
ENV["JUPYTER"]=Sys.which("jupyter"); 

using Pkg
Pkg.add(["IJulia", "PackageCompiler"]);
        # Install test dependencies for IJulia 
Pkg.add([PackageSpec(name="JSON", version="0.21.0"),
        # Install test dependencies for Plots v1.6.0
        PackageSpec(name="Plots", version="1.6.0"),
        PackageSpec(name="ImageMagick", version="1.1.6"),
        PackageSpec(name="VisualRegressionTests", version="1.0.0"),
        PackageSpec(name="FileIO", version="1.4.2"),
        PackageSpec(name="StableRNGs", version="0.1.1"),
        PackageSpec(name="Gtk", version="1.1.4"),
        PackageSpec(name="GeometryTypes", version="0.8.3"),
        PackageSpec(name="GeometryBasics", version="0.2.15"),
        PackageSpec(name="HDF5", version="0.13.5"),
        PackageSpec(name="PGFPlotsX", version="1.2.9"),
        PackageSpec(name="StaticArrays", version="0.12.4"),
        PackageSpec(name="OffsetArrays", version="1.1.3"),
        PackageSpec(name="UnicodePlots", version="1.1.0"),
        PackageSpec(name="Distributions", version="0.23.10"),
])

using Plots
include(joinpath(pkgdir(Plots), "test", "runtests.jl"))

Or download file from this gist and save it as traced.jl

After that, simply run:

using PackageCompiler; create_sysimage([:Plots, :Revise, :OhMyREPL], precompile_statements_file="traced.jl", sysimage_path="plots")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment