Path: blob/main/src/resources/jupyter/lang/julia/setup.jl
12923 views
import IJulia1import Base6423# The julia kernel has built in support for Revise.jl, so this is the4# recommended approach for long-running sessions:5# https://github.com/JuliaLang/IJulia.jl/blob/9b10fa9b879574bbf720f5285029e07758e50a5e/src/kernel.jl#L46-L5167# Users should enable revise within .julia/config/startup_ijulia.jl:8# https://timholy.github.io/Revise.jl/stable/config/#Using-Revise-automatically-within-Jupyter/IJulia-1910# clear console history11IJulia.clear_history()1213fig_width = {fig_width}14fig_height = {fig_height}15fig_format = :{fig_format}16fig_dpi = {fig_dpi}1718# no retina format type, use svg for high quality type/marks19if fig_format == :retina20fig_format = :svg21elseif fig_format == :pdf22fig_dpi = 9623# Enable PDF support for IJulia24IJulia.register_mime(MIME("application/pdf"))25end2627# convert inches to pixels28fig_width = fig_width * fig_dpi29fig_height = fig_height * fig_dpi3031# Intialize Plots w/ default fig width/height32try33import Plots3435# Plots.jl doesn't support PDF output for versions < 1.28.136# so use png (if the DPI remains the default of 300 then set to 96)37if (Plots._current_plots_version < v"1.28.1") & (fig_format == :pdf)38Plots.gr(size=(fig_width, fig_height), fmt = :png, dpi = fig_dpi)39else40Plots.gr(size=(fig_width, fig_height), fmt = fig_format, dpi = fig_dpi)41end42catch e43# @warn "Plots init" exception=(e, catch_backtrace())44end4546# Initialize CairoMakie with default fig width/height47try48import CairoMakie4950# CairoMakie's display() in PDF format opens an interactive window51# instead of saving to the ipynb file, so we don't do that.52# https://github.com/quarto-dev/quarto-cli/issues/754853if fig_format == :pdf54CairoMakie.activate!(type = "png")55else56CairoMakie.activate!(type = string(fig_format))57end58CairoMakie.update_theme!(resolution=(fig_width, fig_height))59catch e60# @warn "CairoMakie init" exception=(e, catch_backtrace())61end6263# Set run_path if specified64try65run_path = "{run_path}"66if !isempty(run_path)67run_path = String(Base64.base64decode(run_path))68cd(run_path)69end70catch e71@warn "Run path init:" exception=(e, catch_backtrace())72end737475# emulate old Pkg.installed beahvior, see76# https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/977import Pkg78function isinstalled(pkg::String)79any(x -> x.name == pkg && x.is_direct_dep, values(Pkg.dependencies()))80end8182# ojs_define83if isinstalled("JSON") && isinstalled("DataFrames")84import JSON, DataFrames85global function ojs_define(; kwargs...)86convert(x) = x87convert(x::DataFrames.AbstractDataFrame) = Tables.rows(x)88content = Dict("contents" => [Dict("name" => k, "value" => convert(v)) for (k, v) in kwargs])89tag = "<script type='ojs-define'>$(JSON.json(content))</script>"90IJulia.display(MIME("text/html"), tag)91end92elseif isinstalled("JSON")93import JSON94global function ojs_define(; kwargs...)95content = Dict("contents" => [Dict("name" => k, "value" => v) for (k, v) in kwargs])96tag = "<script type='ojs-define'>$(JSON.json(content))</script>"97IJulia.display(MIME("text/html"), tag)98end99else100global function ojs_define(; kwargs...)101@warn "JSON package not available. Please install the JSON.jl package to use ojs_define."102end103end104105106# don't return kernel dependencies (b/c Revise should take care of dependencies)107nothing108109110