Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions mathicsscript/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@
Format Mathics objects
"""

import random
import math
import networkx as nx
import random
from tempfile import NamedTemporaryFile

try:
import matplotlib.pyplot as plt
except ImportError:
plt = None

try:
import matplotlib.image as mpimg
except ImportError:
mpimg = None

try:
from cairosvg import svg2png
except ImportError:
svg2png = None


def format_output(obj, expr, format=None):
def eval_boxes(result, fn, obj, **options):
try:
boxes = fn(evaluation=obj, **options)
except BoxError:
boxes = None
if not hasattr(obj, "seen_box_error"):
obj.seen_box_error = True
obj.message(
"General", "notboxes", Expression("FullForm", result).evaluate(obj)
)

return boxes

if format is None:
format = obj.format

Expand All @@ -28,12 +57,20 @@ def format_output(obj, expr, format=None):
if len(leaves) == 1:
expr = leaves[0]
elif expr_type in ("System`Graphics", "System`Plot"):
result = "-System Graphics-"
result = Expression("StandardForm", expr)
result = result.format(obj, "System`MathMLForm")
# ml_str = result.leaves[0].leaves[0]
# FIXME: not quite right. Need to parse out strings
# display_svg(str(ml_str))
form_expr = Expression("StandardForm", expr)
result = form_expr.format(obj, "System`StandardForm")
svg_str = eval_boxes(result, result.boxes_to_svg, obj)
if plt:
temp_png = NamedTemporaryFile(
mode="w+b", suffix=".png", prefix="mathicsscript-"
)
svg2png(bytestring=svg_str, write_to=temp_png.name)
plt.axes().set_axis_off()
img = mpimg.imread(temp_png)
plt.imshow(img)
plt.show()
temp_png.close()
return expr_type

if format == "text":
result = expr.format(obj, "System`OutputForm")
Expand Down Expand Up @@ -359,7 +396,6 @@ def format_graph(G):
Format a Graph
"""
# FIXME handle graphviz as well
import matplotlib.pyplot as plt

global node_size
global cached_pair
Expand Down
2 changes: 0 additions & 2 deletions requirements-extra.txt

This file was deleted.

3 changes: 3 additions & 0 deletions requirements-full.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PyYAML # Used for admin-tools/make-tables.sh to build JSON tables
PyQT5 # For interactive display of graphs via matplotlib
cairosvg # For rendering Plots and Graphs as SVGs via matplotlib