JerryI / wolfram-js-frontend

Dynamic Notebook Environment for Wolfram Language written in Javascript
https://jerryi.github.io/wljs-docs/
GNU General Public License v3.0
304 stars 13 forks source link

Lack some implements #3

Closed asukaminato0721 closed 1 year ago

asukaminato0721 commented 1 year ago

image

but it can be a frontend :smile:

RowBox, BoxData, and some underlying layout functions.

maybe there can even be some jsx/tsx or frontend template to semi-auto interpret the string to the dom tree. But using a frontend framework is a double-edged sword.

asukaminato0721 commented 1 year ago

Amazing, I only installed the wolfram engine, but what is this?

image

asukaminato0721 commented 1 year ago

also, I noticed there may miss some error handling. image

so after running

GeneralUtilities`PrintDefinitionsLocal[Sin] // HTMLForm

there will be a pop up said this:

image

JerryI commented 1 year ago

Hah. Don't worry, there will be a major update soon, where i accumulated all bug fixes :D

JerryI commented 1 year ago

Amazing, I only installed the wolfram engine, but what is this?

image

This is some standard behaviour, when you interrupt the kernel on Linux with GUI. Just click ok)

JerryI commented 1 year ago

A long time ago I tried to implement low-level notebook cells functions. Well, I would say looking back to the code it was a huge mess. Since it is also not well-documented, hard to imagine how it can be inspected.

I think, in this sense, the immutable objects like this can be a good compromise - looks still fancy, but fully editable using a single instance of the CodeMirror Screenshot 2023-02-02 at 21 16 43

JerryI commented 1 year ago

I added a new option to the env variable, now we have

In principle, using this we can reproduce the results from Mathematica's frontend. Like mixing rational numbers and graphics

Table[(i+j) /j Graphics3D[{RGBColor[i/2,j/2,1], Sphere[{1/2,1/2,1/2}, 0.6], Cuboid[]}], {i,1,2}, {j,1,2}]//TableForm

It already works. I tried to implement it with core.Times and core.Rational as a proof of concept.

userrand commented 1 year ago

image

but it can be a frontend smile

RowBox, BoxData, and some underlying layout functions.

maybe there can even be some jsx/tsx or frontend template to semi-auto interpret the string to the dom tree. But using a frontend framework is a double-edged sword.

DisplayForm followed by TeXForm can transform some of the row boxes to LaTeX. Then one can maybe just render the LaTeX to HTML using MathJax or something (or if you already have something that converts Graphics to HTML then maybe MaTeX https://resources.wolframcloud.com/FunctionRepository/resources/MaTeXInstall/ ). However, the Box expression to LaTeX converter is not perfect (in particular it ignores Style usually) but maybe it is easier to fix a few cases of the LaTeX converter than to implement the entire box structure into HTML from scratch.

LyX uses a WYSIWYG approach to render in real time, modify the LaTeX and copy parts of an expression as pure latex. That could be convenient to use instead of boxes if it is easy to extract the relevant code from LyX or if one of these repositories: https://github.com/search?q=wysiwyg+latex works well and is easy to use. As shown here : https://tex.stackexchange.com/a/79097 one can include images in an equation so it might be possible to have some of the flexibility of using boxes using a WYSIWYG latex editor. Animations are also possible as they can be used with beamer for example here https://tex.stackexchange.com/questions/55216/tikz-animated-equation-in-beamer

For the example given by @wuyudi one can use the printdef function below instead of GeneralUtilities`PrintDefinitionsLocal

 printdef[args___]:=Reap[Block[{CreateDocument = 
 Function[Sow@#]}, GeneralUtilities`PrintDefinitions @ args]][[2,1]]//DisplayForm 

I used a "CreateDocument =..." trick that I found using GeneralUtilitiesPrintDefinitions on GeneralUtilitiesPrintDefinitionsLocal.

With Wolfram language for jupyter the output is automatically rasterized. One can obtain the output in LaTeX using printdef[Sin]//TeXForm

With longer outputs with hyperlinks it might be better to use the rasterized image or convert the output to HTML (there are wolfram notebook to jupyter converters if I am not mistaken, so maybe some of the code from those converters could be used)

userrand commented 1 year ago

A mathematica notebook to jupyter converter that might help (not sure did not check the code nor use it)

https://mathematica.stackexchange.com/a/210938

A mathematica notebook to markdown converter that might help (not sure)

https://mathematica.stackexchange.com/a/100127

JerryI commented 1 year ago

but it can be a frontend 😄

RowBox, BoxData, and some underlying layout functions.

maybe there can even be some jsx/tsx or frontend template to semi-auto interpret the string to the dom tree. But using a frontend framework is a double-edged sword.

Well, @wuyudi, In the end I decided to implement those boxes by spawning a nested editor inside each box

Screenshot 2023-04-24 at 10 04 59

Surprisingly it has no input lag, thanks @marijnh for 14 years of development of CodeMirror

In principle everything works, but I have no idea how to implement parentheses, that can adjust their height according to the nested expression

And the navigation using arrows now is also problematic, one have to click manually on each box

userrand commented 1 year ago

but it can be a frontend smile RowBox, BoxData, and some underlying layout functions. maybe there can even be some jsx/tsx or frontend template to semi-auto interpret the string to the dom tree. But using a frontend framework is a double-edged sword.

Well, @wuyudi, In the end I decided to implement those boxes by spawning a nested editor inside each box

Screenshot 2023-04-24 at 10 04 59

Surprisingly it has no input lag, thanks @marijnh for 14 years of development of CodeMirror

In principle everything works, but I have no idea how to implement parentheses, that can adjust their height according to the nested expression

And the navigation using arrows now is also problematic, one have to click manually on each box

Why is the $\beta$ in the numerator in the output when it is in the denominator in the input?

userrand commented 1 year ago

The output from codemirror is MathML?

JerryI commented 1 year ago

Ouups. I flipped those by accident. ;)

The output from codemirror is MathML?

Actually this is sort of Box output. I basically take the DisplayForm of mathematica output, remove some overcomplicated stuff like TagBox, RowBox leaving out only fractions, matrixes and send to the editor.

On the editor's side regular expressions are looking for Subscript, Sqrt, FractionBox functions and decorates them with HTML/CSS creating a new editor inside (I had to implement a small WL parser), i.e. you can edit everything like in Mathematica. The depth of nested editors is not limited. Each editor updates the content it hides each time you type anything from top to bottom. So one copy them from the cell, it will show pure WL code.

Snippets and hotkeys (Ctrl+/ to create fractions) are supported.

So this is a somewhat a compromise between Mathematica's Boxs, where the code is held in a string and InputForm.

PS: But yes, firstly I was experimenting with MathML and MaTeX, I stole CSS styles from them and it gave me a hit on how to process the data from DisplayForm

JerryI commented 1 year ago

example.

what you see Screenshot 2023-04-24 at 14 08 36

inside

Table[i, {i,1}]; Sqrt[Fraction[1, 2]] + Subscript[x, y_ ] 

where only Fraction is unknown expression to Mathematica, but on input it can be replaced Fraction -> Function[{x,y}, x/y] by the preprocessor

UPD: I have separated it as a package and published it on NPM

JerryI commented 1 year ago

Looks like it is done