Skip to main content
added 338 characters in body
Source Link
Julien Kluge
  • 5.5k
  • 1
  • 18
  • 30

This is not an anwser to produce live/production code for websides. (yet)

I guess it's not what you want because its not very sophisticated but one could use MMA to make a good html code emitter directly from Mathematica Expressions.

We just write a function html which repeatedly calls itself on the subexpressions till it reaches "atomics" we can directly translate to html and afterward StringJoin'em.

I made a small proof of concept. Theoretically this could be made into a huge library with direct intrinsic functions and normal Mathematica expressions. (The code supports strings, unordered lists, Grids and graphics(with auto export))

html[params__] := 
 StringJoin["<html>\n<body>\n", html /@ List[params], 
  "\n</body>\n</html>\n"]

html[param_String] := param

html[param_List] := 
 StringJoin["\n<ul>\n", 
  StringJoin["<li>", #, "</li>\n"] & /@ (html /@ param), "</ul>\n"]

html[param_Grid] := Module[{data = param[[1]]},
  StringJoin["<table>\n", Table[
    StringJoin["<tr>\n", 
     Table[StringJoin["<td>", html[data[[rows, columns]]], 
       "</td>\n"], {columns, 1, Length[data[[rows]]]}], "</tr>\n"]
    , {rows, 1, Length[data]}], "</table>"]
  ]

html[param_Graphics] := 
 Module[{name = StringJoin[CreateUUID["plot-"], ".png"]}, 
  Export[name, param]; StringJoin["<img src=\"", name, "\"></img>"]]

html[param_] := ToString[param, InputForm]

This can be called with as many Mathematica expressions you want to. At example:

html[
 "The integral of x^2 from 0 to 1 is ",
 Integrate[x^2, {x, 0, 1}],
 "\nfrom 0 to 2, 3, or 4 the integral evaluates to",
 Table[Integrate[x^2, {x, 0, i}], {i, 2, 4}],
 "\n\nThe values from sin(x) in an inteval of 0 to 3 in 0.1 steps are \
(rounded to 10^-4)",
 Grid[Transpose[Table[{x, N@Round[Sin[x], 10^-4]}, {x, 0, 3, 1/3}]]],
 "\n\nThe Plot for cos(x) is\n\n",
 Plot[Cos[x], {x, 0, 2 Pi}]
 ]

generates the coresponding code to create this webside:

The generated webside

The generated code for this is:

generated code

Annother example

functions = {Sin, Cos, Tan, Sqrt, RiemannSiegelZ, LegendreP[2, #] &};
html[
 Grid@Table[{functions[[i]], 
    Plot[functions[[i]][x], {x, 0, 3}, ImageSize -> 300]}, {i, 1, 
    Length@functions}]
 ]

enter image description here

Also a css style generator could be written into this whole thing. So if you have enough spare time, you could build awesome things in this way. (I feel like, this would give a nice package)

This is not an anwser to produce live/production code for websides. (yet)

I guess it's not what you want because its not very sophisticated but one could use MMA to make a good html code emitter directly from Mathematica Expressions.

We just write a function html which repeatedly calls itself on the subexpressions till it reaches "atomics" we can directly translate to html and afterward StringJoin'em.

I made a small proof of concept. Theoretically this could be made into a huge library with direct intrinsic functions and normal Mathematica expressions. (The code supports strings, unordered lists, Grids and graphics(with auto export))

html[params__] := 
 StringJoin["<html>\n<body>\n", html /@ List[params], 
  "\n</body>\n</html>\n"]

html[param_String] := param

html[param_List] := 
 StringJoin["\n<ul>\n", 
  StringJoin["<li>", #, "</li>\n"] & /@ (html /@ param), "</ul>\n"]

html[param_Grid] := Module[{data = param[[1]]},
  StringJoin["<table>\n", Table[
    StringJoin["<tr>\n", 
     Table[StringJoin["<td>", html[data[[rows, columns]]], 
       "</td>\n"], {columns, 1, Length[data[[rows]]]}], "</tr>\n"]
    , {rows, 1, Length[data]}], "</table>"]
  ]

html[param_Graphics] := 
 Module[{name = StringJoin[CreateUUID["plot-"], ".png"]}, 
  Export[name, param]; StringJoin["<img src=\"", name, "\"></img>"]]

html[param_] := ToString[param, InputForm]

This can be called with as many Mathematica expressions you want to. At example:

html[
 "The integral of x^2 from 0 to 1 is ",
 Integrate[x^2, {x, 0, 1}],
 "\nfrom 0 to 2, 3, or 4 the integral evaluates to",
 Table[Integrate[x^2, {x, 0, i}], {i, 2, 4}],
 "\n\nThe values from sin(x) in an inteval of 0 to 3 in 0.1 steps are \
(rounded to 10^-4)",
 Grid[Transpose[Table[{x, N@Round[Sin[x], 10^-4]}, {x, 0, 3, 1/3}]]],
 "\n\nThe Plot for cos(x) is\n\n",
 Plot[Cos[x], {x, 0, 2 Pi}]
 ]

generates the coresponding code to create this webside:

The generated webside

The generated code for this is:

generated code

Also a css style generator could be written into this whole thing. So if you have enough spare time, you could build awesome things in this way. (I feel like, this would give a nice package)

This is not an anwser to produce live/production code for websides. (yet)

I guess it's not what you want because its not very sophisticated but one could use MMA to make a good html code emitter directly from Mathematica Expressions.

We just write a function html which repeatedly calls itself on the subexpressions till it reaches "atomics" we can directly translate to html and afterward StringJoin'em.

I made a small proof of concept. Theoretically this could be made into a huge library with direct intrinsic functions and normal Mathematica expressions. (The code supports strings, unordered lists, Grids and graphics(with auto export))

html[params__] := 
 StringJoin["<html>\n<body>\n", html /@ List[params], 
  "\n</body>\n</html>\n"]

html[param_String] := param

html[param_List] := 
 StringJoin["\n<ul>\n", 
  StringJoin["<li>", #, "</li>\n"] & /@ (html /@ param), "</ul>\n"]

html[param_Grid] := Module[{data = param[[1]]},
  StringJoin["<table>\n", Table[
    StringJoin["<tr>\n", 
     Table[StringJoin["<td>", html[data[[rows, columns]]], 
       "</td>\n"], {columns, 1, Length[data[[rows]]]}], "</tr>\n"]
    , {rows, 1, Length[data]}], "</table>"]
  ]

html[param_Graphics] := 
 Module[{name = StringJoin[CreateUUID["plot-"], ".png"]}, 
  Export[name, param]; StringJoin["<img src=\"", name, "\"></img>"]]

html[param_] := ToString[param, InputForm]

This can be called with as many Mathematica expressions you want to. At example:

html[
 "The integral of x^2 from 0 to 1 is ",
 Integrate[x^2, {x, 0, 1}],
 "\nfrom 0 to 2, 3, or 4 the integral evaluates to",
 Table[Integrate[x^2, {x, 0, i}], {i, 2, 4}],
 "\n\nThe values from sin(x) in an inteval of 0 to 3 in 0.1 steps are \
(rounded to 10^-4)",
 Grid[Transpose[Table[{x, N@Round[Sin[x], 10^-4]}, {x, 0, 3, 1/3}]]],
 "\n\nThe Plot for cos(x) is\n\n",
 Plot[Cos[x], {x, 0, 2 Pi}]
 ]

generates the coresponding code to create this webside:

The generated webside

The generated code for this is:

generated code

Annother example

functions = {Sin, Cos, Tan, Sqrt, RiemannSiegelZ, LegendreP[2, #] &};
html[
 Grid@Table[{functions[[i]], 
    Plot[functions[[i]][x], {x, 0, 3}, ImageSize -> 300]}, {i, 1, 
    Length@functions}]
 ]

enter image description here

Also a css style generator could be written into this whole thing. So if you have enough spare time, you could build awesome things in this way. (I feel like, this would give a nice package)

Source Link
Julien Kluge
  • 5.5k
  • 1
  • 18
  • 30

This is not an anwser to produce live/production code for websides. (yet)

I guess it's not what you want because its not very sophisticated but one could use MMA to make a good html code emitter directly from Mathematica Expressions.

We just write a function html which repeatedly calls itself on the subexpressions till it reaches "atomics" we can directly translate to html and afterward StringJoin'em.

I made a small proof of concept. Theoretically this could be made into a huge library with direct intrinsic functions and normal Mathematica expressions. (The code supports strings, unordered lists, Grids and graphics(with auto export))

html[params__] := 
 StringJoin["<html>\n<body>\n", html /@ List[params], 
  "\n</body>\n</html>\n"]

html[param_String] := param

html[param_List] := 
 StringJoin["\n<ul>\n", 
  StringJoin["<li>", #, "</li>\n"] & /@ (html /@ param), "</ul>\n"]

html[param_Grid] := Module[{data = param[[1]]},
  StringJoin["<table>\n", Table[
    StringJoin["<tr>\n", 
     Table[StringJoin["<td>", html[data[[rows, columns]]], 
       "</td>\n"], {columns, 1, Length[data[[rows]]]}], "</tr>\n"]
    , {rows, 1, Length[data]}], "</table>"]
  ]

html[param_Graphics] := 
 Module[{name = StringJoin[CreateUUID["plot-"], ".png"]}, 
  Export[name, param]; StringJoin["<img src=\"", name, "\"></img>"]]

html[param_] := ToString[param, InputForm]

This can be called with as many Mathematica expressions you want to. At example:

html[
 "The integral of x^2 from 0 to 1 is ",
 Integrate[x^2, {x, 0, 1}],
 "\nfrom 0 to 2, 3, or 4 the integral evaluates to",
 Table[Integrate[x^2, {x, 0, i}], {i, 2, 4}],
 "\n\nThe values from sin(x) in an inteval of 0 to 3 in 0.1 steps are \
(rounded to 10^-4)",
 Grid[Transpose[Table[{x, N@Round[Sin[x], 10^-4]}, {x, 0, 3, 1/3}]]],
 "\n\nThe Plot for cos(x) is\n\n",
 Plot[Cos[x], {x, 0, 2 Pi}]
 ]

generates the coresponding code to create this webside:

The generated webside

The generated code for this is:

generated code

Also a css style generator could be written into this whole thing. So if you have enough spare time, you could build awesome things in this way. (I feel like, this would give a nice package)