Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #14953: Moving the .html to EXT_CODE and using DOCTEST_MODE
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanncohen committed Jun 2, 2014
1 parent 9cbcc36 commit 76acf85
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
File renamed without changes.
14 changes: 11 additions & 3 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15537,7 +15537,7 @@ def show(self, method = "matplotlib", **kwds):
- If ``method="js"`` the graph is displayed using the `d3.js
<http://d3js.org/>`_ library in a browser. In this situation, the
method accepts any other option understood by
:meth:`sage.graphs.graph_plot_js.show`.
:meth:`sage.graphs.graph_plot_js.gen_html_code`.


This method accepts any other option understood by
Expand All @@ -15559,8 +15559,16 @@ def show(self, method = "matplotlib", **kwds):
sage: P.show() # long time (3s on sage.math, 2011)
"""
if method == "js":
from sage.graphs.graph_plot_js import show
return show(self, **kwds)
from sage.graphs.graph_plot_js import gen_html_code
from sage.doctest import DOCTEST_MODE
filename = gen_html_code(self, **kwds)

if DOCTEST_MODE:
return
from sage.misc.viewer import browser
import os
os.system('%s %s 2>/dev/null 1>/dev/null &'% (browser(), filename))
return

from graph_plot import graphplot_options

Expand Down
56 changes: 31 additions & 25 deletions src/sage/graphs/graph_plot_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@
- ``color`` -- color (hexadecimal code)
-``curve`` -- distance from the barycenter of the two endpoints and the
- ``curve`` -- distance from the barycenter of the two endpoints and the
center of the edge. It defines the curve of the edge, which can be useful
for multigraphs.
- ``pos`` -- a list whose `i` th element is a dictionary defining the position of
the `i` th vertex.
It also contains the definition of some numerical/boolean variables whose
definition can be found in the documentation of :meth:`show` : ``directed``,
``charge``, ``link_distance``, ``link_strength``, ``gravity``, ``vertex_size``,
definition can be found in the documentation of
:meth:`~sage.graphs.generic_graph.GenericGraph.show` : ``directed``, ``charge``,
``link_distance``, ``link_strength``, ``gravity``, ``vertex_size``,
``edge_thickness``.
.. TODO::
Expand All @@ -61,7 +62,6 @@
"""
from sage.misc.temporary_file import tmp_filename
from sage.plot.colors import rainbow
import os

#*****************************************************************************
# Copyright (C) 2013 Nathann Cohen <nathann.cohen@gmail.com>
Expand All @@ -74,20 +74,24 @@
#*****************************************************************************


def show(G,
vertex_labels=False,
edge_labels=False,
vertex_partition=[],
edge_partition=[],
force_spring_layout=False,
charge=-120,
link_distance=30,
link_strength=2,
gravity=.04,
vertex_size=7,
edge_thickness=4):
def gen_html_code(G,
vertex_labels=False,
edge_labels=False,
vertex_partition=[],
edge_partition=[],
force_spring_layout=False,
charge=-120,
link_distance=30,
link_strength=2,
gravity=.04,
vertex_size=7,
edge_thickness=4):
r"""
Displays the graph in a browser using `d3.js <http://d3js.org/>`_.
Creates a .html file showing the graph using `d3.js <http://d3js.org/>`_.
This function returns the name of the .html file. If you want to
visualize the actual graph use
:meth:`~sage.graphs.generic_graph.GenericGraph.show`.
INPUT:
Expand All @@ -107,8 +111,9 @@ def show(G,
instead. Set to ``[]`` by default.
- ``force_spring_layout`` -- whether to take sage's position into account if
there is one (see :meth:`~Graph.get_pos` and :meth:`~Graph.set_pos`), or
to compute a spring layout. Set to ``False`` by default.
there is one (see :meth:`~sage.graphs.generic_graph.GenericGraph.` and
:meth:`~sage.graphs.generic_graph.GenericGraph.`), or to compute a spring
layout. Set to ``False`` by default.
- ``vertex_size`` -- The size of a vertex' circle. Set to `7` by default.
Expand Down Expand Up @@ -153,6 +158,10 @@ def show(G,
....: edge_partition=[[("11","12","2"),("21","21","a")]],
....: edge_thickness=4) # optional -- internet
TESTS::
sage: from sage.graphs.graph_plot_js import gen_html_code
sage: filename = gen_html_code(graphs.PetersenGraph())
"""
directed = G.is_directed()
multiple_edges = G.has_multiple_edges()
Expand Down Expand Up @@ -255,8 +264,8 @@ def show(G,
"vertex_size": int(vertex_size),
"edge_thickness": int(edge_thickness)})

from sage.misc.misc import SAGE_SRC
js_code_file = open(SAGE_SRC+"/sage/graphs/graph_plot_js.html", 'r')
from sage.env import SAGE_EXTCODE
js_code_file = open(SAGE_EXTCODE+"/graphs/graph_plot_js.html", 'r')
js_code = js_code_file.read().replace("// HEREEEEEEEEEEE", string)
js_code_file.close()

Expand All @@ -266,7 +275,4 @@ def show(G,
f.write(js_code)
f.close()

# Opens the browser
from sage.misc.viewer import browser
os.system('%s %s 2>/dev/null 1>/dev/null &'
% (browser(), filename))
return filename

0 comments on commit 76acf85

Please sign in to comment.