Simon Danisch / Oct 07 2019

WGLMakie in Python

import julia
from julia.api import Julia
jl = Julia(compiled_modules=False)
from julia import Base
from julia import Main
from julia import AbstractPlotting
from julia import WGLMakie
from julia import JSServe
class Plot:
  def __init__(self):
    # Setup Julia
    Main.eval("""
        url = strip(ENV["NEXTJOURNAL_RUNTIME_SERVICE_URL"])
        ENV["JULIA_WEBIO_BASEURL"] = url
        ENV["WEBIO_SERVER_HOST_URL"] = "0.0.0.0"
        ENV["WEBIO_HTTP_PORT"] = 9998
        surl = replace(url, "http" => "ws") * "/webio_websocket/"
        ENV["WEBIO_WEBSOCKT_URL"] = surl
        JSServe.__init__()
		""")
    self.func = None
    self.plot = None
    
  def __getattr__(self, symbol):
    # Return Julia function
    if Main.eval(f"isdefined(AbstractPlotting, :{symbol})"):
      self.func = Main.eval(f"getfield(AbstractPlotting, :{symbol})")
      return self
    else:
      return None
    
  def __call__(self, *args, **kwargs):
    if (self.func != None):
      self.plot = self.func(*args, **kwargs)
    return self
  def _repr_webio_(self):
    if (self.plot != None):
      return Base.repr(
        "application/vnd.webio.application+html", 
        self.plot
      ).decode("utf-8")
    else: 
      return ""
M = Plot()
import numpy as np
X = np.arange(-2, 2, 0.25)
Y = np.arange(-2, 2, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
M.surface(X, Y, Z, resolution = (500, 400), colormap = "magma")
while True:
	Main.eval("yield()")