Skip to content

Commit

Permalink
Refactor output on desktop to F#show() method
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Jul 29, 2019
1 parent ed6623c commit 097fa80
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,86 @@ public static boolean isFileSystemEnabled(EvalEngine engine) {
* Use visjs.org JavaScript library for visualizing graph theory objects
*/
public static boolean USE_VISJS = false;

/**
* HTML template for the <a href="https://visjs.org/">VIS-network</a>
*/
public final static String VISJS_PAGE = //
"<html>\n" + //
"<head>\n" + //
"<meta charset=\"utf-8\">\n" + //
"<head>\n" + //
" <title>VIS-Network</title>\n" + //
"\n" + //
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/vis-network.min.js\"></script>\n"
+ //
" <style type=\"text/css\">\n" + //
" #mynetwork {\n" + //
" width: 600px;\n" + //
" height: 400px;\n" + //
" border: 1px solid lightgray;\n" + //
" }\n" + //
" </style>\n" + //
"</head>\n" + //
"<body>\n" + //
"\n" + //
"<h1>VIS-Network</h1>\n" + //
"\n" + //
"<div id=\"vis\"></div>\n" + //
"\n" + //
"<script type=\"text/javascript\">\n" + //
"`1`\n" + //
" // create a network\n" + //
" var container = document.getElementById('vis');\n" + //
" var data = {\n" + //
" nodes: nodes,\n" + //
" edges: edges\n" + //
" };\n" + //
" var options = {};\n" + //
" var network = new vis.Network(container, data, options);\n" + //
"</script>\n" + //
"\n" + //
"\n" + //
"</body>\n" + //
"</html>";//

/**
* HTML template for the <a href="https://github.com/paulmasson/mathcell">MathCell</a> and
* <a href="https://github.com/paulmasson/math">Math</a> JavaScript libraries.
*/
public final static String MATHCELL_PAGE = //
"<html>\n" + //
"<head>\n" + //
"<meta charset=\"utf-8\">\n" + //
"<title>MathCell</title>\n" + //
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + //
"<style></style>\n" + //
"</head>\n" + //
"\n" + //
"<body>\n" + //
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/[email protected]/build/math.js\"></script>" + //
"\n" + //
"\n" + //
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/[email protected]/build/mathcell.js\"></script>\n"
+ //
"<script src=\"https://cdn.jsdelivr.net/gh/mathjax/[email protected]/MathJax.js?config=TeX-AMS_HTML\"></script>"
+ //
"\n" + //
"<div class=\"mathcell\" style=\"width: 100%; height: 100%; padding: .25in .5in .5in .5in;\">\n" + //
"<script>\n" + //
"\n" + //
"var parent = document.scripts[ document.scripts.length - 1 ].parentNode;\n" + //
"\n" + //
"var id = generateId();\n" + //
"parent.id = id;\n" + //
"\n" + //
"`1`\n" + //
"\n" + //
"parent.update( id );\n" + //
"\n" + //
"</script>\n" + //
"</div>\n" + //
"\n" + //
"</body>\n" + //
"</html>";//
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import org.matheclipse.core.basic.Config;
import org.matheclipse.core.builtin.GraphFunctions;
import org.matheclipse.core.eval.exception.AbortException;
import org.matheclipse.core.eval.exception.ReturnException;
import org.matheclipse.core.eval.exception.Validate;
Expand All @@ -27,7 +23,6 @@
import org.matheclipse.core.form.output.OutputFormFactory;
import org.matheclipse.core.graphics.Show2SVG;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IDataExpr;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.Scanner;
import org.matheclipse.parser.client.SyntaxError;
Expand All @@ -39,80 +34,6 @@
* See {@link MMAConsole}
*/
public class Console {
protected final static String VISJS_PAGE = //
"<html>\n" + //
"<head>\n" + //
"<meta charset=\"utf-8\">\n" + //
"<head>\n" + //
" <title>VIS-Network</title>\n" + //
"\n" + //
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/vis-network.min.js\"></script>\n"
+ //
" <style type=\"text/css\">\n" + //
" #mynetwork {\n" + //
" width: 600px;\n" + //
" height: 400px;\n" + //
" border: 1px solid lightgray;\n" + //
" }\n" + //
" </style>\n" + //
"</head>\n" + //
"<body>\n" + //
"\n" + //
"<h1>VIS-Network</h1>\n" + //
"\n" + //
"<div id=\"vis\"></div>\n" + //
"\n" + //
"<script type=\"text/javascript\">\n" + //
"`1`\n" + //
" // create a network\n" + //
" var container = document.getElementById('vis');\n" + //
" var data = {\n" + //
" nodes: nodes,\n" + //
" edges: edges\n" + //
" };\n" + //
" var options = {};\n" + //
" var network = new vis.Network(container, data, options);\n" + //
"</script>\n" + //
"\n" + //
"\n" + //
"</body>\n" + //
"</html>";//
protected final static String MATHCELL_PAGE = //
"<html>\n" + //
"<head>\n" + //
"<meta charset=\"utf-8\">\n" + //
"<title>MathCell</title>\n" + //
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + //
"<style></style>\n" + //
"</head>\n" + //
"\n" + //
"<body>\n" + //
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/[email protected]/build/math.js\"></script>" + //
"\n" + //
"\n" + //
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/[email protected]/build/mathcell.js\"></script>\n"
+ //
"<script src=\"https://cdn.jsdelivr.net/gh/mathjax/[email protected]/MathJax.js?config=TeX-AMS_HTML\"></script>"
+ //
"\n" + //
"<div class=\"mathcell\" style=\"width: 100%; height: 100%; padding: .25in .5in .5in .5in;\">\n" + //
"<script>\n" + //
"\n" + //
"var parent = document.scripts[ document.scripts.length - 1 ].parentNode;\n" + //
"\n" + //
"var id = generateId();\n" + //
"parent.id = id;\n" + //
"\n" + //
"`1`\n" + //
"\n" + //
"parent.update( id );\n" + //
"\n" + //
"</script>\n" + //
"</div>\n" + //
"\n" + //
"</body>\n" + //
"</html>";//

/**
* 60 seconds timeout limit as the default value for Symja expression evaluation.
*/
Expand Down Expand Up @@ -590,35 +511,9 @@ private String printResult(IExpr result) throws IOException {
if (result.isAST(F.Graphics)) {// || result.isAST(F.Graphics3D)) {
outExpr = F.Show(outExpr);
}
if (outExpr.isSameHeadSizeGE(F.Show, 2)) {
try {
IAST show = (IAST) outExpr;
if (show.size() > 1 && show.arg1().isSameHeadSizeGE(F.Graphics, 2)) {
return openSVGOnDesktop(show);
}
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
} else if (result.head().equals(F.Graph) && result instanceof IDataExpr) {
String javaScriptStr = GraphFunctions.graphToJSForm((IDataExpr) result);
if (javaScriptStr != null) {
String html = VISJS_PAGE;
html = html.replaceAll("`1`", javaScriptStr);
return Console.openHTMLOnDesktop(html);
}
} else if (result.isAST(F.JSFormData, 3) && result.second().toString().equals("mathcell")) {
try {
String manipulateStr = ((IAST) result).arg1().toString();
String html = MATHCELL_PAGE;
html = html.replaceAll("`1`", manipulateStr);
return Console.openHTMLOnDesktop(html);
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
String html = F.show(outExpr);
if (html != null) {
return html;
}
}
StringBuilder strBuffer = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,36 +578,10 @@ private String printResult(IExpr result) throws IOException {
if (result.isAST(F.Graphics)) {// || result.isAST(F.Graphics3D)) {
outExpr = F.Show(outExpr);
}
if (outExpr.isSameHeadSizeGE(F.Show, 2)) {
try {
IAST show = (IAST) outExpr;
if (show.size() > 1 && show.arg1().isSameHeadSizeGE(F.Graphics, 2)) {
return Console.openSVGOnDesktop(show);
}
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
} else if (result.head().equals(F.Graph) && result instanceof IDataExpr) {
String javaScriptStr = GraphFunctions.graphToJSForm((IDataExpr) result);
if (javaScriptStr != null) {
String html = Console.VISJS_PAGE;
html = html.replaceAll("`1`", javaScriptStr);
return Console.openHTMLOnDesktop(html);
}
} else if (result.isAST(F.JSFormData, 3) && result.second().toString().equals("mathcell")) {
try {
String manipulateStr = ((IAST) result).arg1().toString();
String html = Console.MATHCELL_PAGE;
html = html.replaceAll("`1`", manipulateStr);
return Console.openHTMLOnDesktop(html);
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
}
String html = F.show(outExpr);
if (html != null) {
return html;
}
}

StringBuilder strBuffer = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.matheclipse.core.expression;

import java.io.IOException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -62,6 +63,7 @@
import org.matheclipse.core.builtin.WindowFunctions;
import org.matheclipse.core.convert.AST2Expr;
import org.matheclipse.core.convert.Object2Expr;
import org.matheclipse.core.eval.Console;
import org.matheclipse.core.eval.EvalAttributes;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.interfaces.AbstractCoreFunctionEvaluator;
Expand All @@ -76,6 +78,7 @@
import org.matheclipse.core.interfaces.IBuiltInSymbol;
import org.matheclipse.core.interfaces.IComplex;
import org.matheclipse.core.interfaces.IComplexNum;
import org.matheclipse.core.interfaces.IDataExpr;
import org.matheclipse.core.interfaces.IEvaluator;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.IFraction;
Expand Down Expand Up @@ -9164,4 +9167,53 @@ public static IExpr fromString(String string) {
return stringx(string);
}

/**
* Show the result in an HTML page with the help of the Java <code>Desktop.getDesktop().open()</code> method. On
* some platforms the Desktop API may not be supported; use the <code>isDesktopSupported()</code> method todetermine
* if the current desktop is supported.
*
* @param expr
* @return
* @throws IOException
*/
public static String show(IExpr expr) {
if (expr.isSameHeadSizeGE(Show, 2)) {
try {
IAST show = (IAST) expr;
if (show.size() > 1 && show.arg1().isSameHeadSizeGE(Graphics, 2)) {
return Console.openSVGOnDesktop(show);
}
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
} else if (expr.head().equals(Graph) && expr instanceof IDataExpr) {
String javaScriptStr = GraphFunctions.graphToJSForm((IDataExpr) expr);
if (javaScriptStr != null) {
try {
String html = Config.VISJS_PAGE;
html = html.replaceAll("`1`", javaScriptStr);
return Console.openHTMLOnDesktop(html);
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
}
} else if (expr.isAST(JSFormData, 3) && expr.second().toString().equals("mathcell")) {
try {
String manipulateStr = ((IAST) expr).arg1().toString();
String html = Config.MATHCELL_PAGE;
html = html.replaceAll("`1`", manipulateStr);
return Console.openHTMLOnDesktop(html);
} catch (Exception ex) {
if (Config.SHOW_STACKTRACE) {
ex.printStackTrace();
}
}
}
return null;
}

}

0 comments on commit 097fa80

Please sign in to comment.