Skip to content

AtelierArith/PlutoMonacoEditor.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

This repository provides an editor for writing non-Julia source code that can be used with @bind in Pluto.jl. This editor is the Monaco Editor itself. If you want to write code on top of Pluto Notebook for non-Julia source code, try this repository.

image

Python editor

julia> # TL;DR
julia> using Pluto; Pluto.run(notebook="examples/python.jl")

Prepare the following code to set up an instance of Monaco Editor.

begin
	using CondaPkg
	CondaPkg.add_pip("numpy")
	using PythonCall
end
begin
	initCode = """
	import numpy as np
	def myapi():
		return np.random.random((2, 3))

	"""
	@bind pythoncode MonacoEditor("python", initCode)
end

The @bind macro connects the value of MonacoEditor("pythno", initCode) and Julia variable pythoncode. As an application, to compile the source code pythoncode, Add the following code to your Pluto Notebook:

begin
	function loadpythonmodule(pythoncode)
		write("mylib.py", pythoncode)
		importlib = pyimport("importlib")
		pyimport("sys")."path".append(@__DIR__)
		mylib = pyimport("mylib")
		importlib.reload(mylib)
		mylib
	end
	
	mylib = loadpythonmodule(pythoncode)
end

This defines a Python module as mylib. We can call an function in this module with:

println(mylib.myapi())

Rust editor

julia> # TL;DR
julia> using Pluto; Pluto.run(notebook="examples/rust.jl")

Prepare the following code to set up an instance of Monaco Editor.

begin
	initCode = """
	fn main(){
		println!("Hello");
	}
	"""
	@bind rustcode MonacoEditor("rust", initCode)
end

The @bind macro connects the value of MonacoEditor("rust", initCode) and Julia variable rustcode. As an application, to compile the source code rustcode, Add the following code to your Pluto Notebook:

mktempdir() do d
	sourcepath = joinpath(d, "main.rs")
	write(sourcepath, rustcode)
	executablepath = joinpath(d, "main")
	try
		run(`rustc $(sourcepath) -o $(executablepath)`)
		println(readchomp(`$(executablepath)`))
	catch 
	end
end

Appendix

❓ What is the relationship between VS Code and the Monaco Editor?

The Monaco Editor is generated straight from VS Code's sources with some shims around services the code needs to make it run in a web browser outside of its home.

https://github.com/microsoft/monaco-editor?tab=readme-ov-file#faq