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

Commit

Permalink
trac 33931: deprecate sage.misc.viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpalmieri committed May 29, 2022
1 parent 5fb2a6e commit bbe8110
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 53 deletions.
5 changes: 2 additions & 3 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@
# ****************************************************************************

from copy import copy
import webbrowser

from sage.graphs.views import EdgesView
from .generic_graph_pyx import GenericGraph_pyx, spring_layout_fast, layout_split
Expand Down Expand Up @@ -20468,9 +20469,7 @@ def show(self, method="matplotlib", **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))
webbrowser.open(r'file:///{}'.format(filename))
return

from .graph_plot import graphplot_options
Expand Down
16 changes: 3 additions & 13 deletions src/sage/misc/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import random
import re
import shutil
import webbrowser
from subprocess import call, PIPE

from sage.misc.cachefunc import cached_function, cached_method
Expand Down Expand Up @@ -1914,22 +1915,11 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
with open(tex_file, 'w') as file:
file.write(s)
suffix = _run_latex_(tex_file, debug=debug, engine=engine, png=False)
if suffix == "pdf":
from sage.misc.viewer import pdf_viewer
viewer = pdf_viewer()
elif suffix == "dvi":
from sage.misc.viewer import dvi_viewer
viewer = dvi_viewer()
else:
if not (suffix == "pdf" or suffix == "dvi"):
print("Latex error")
return
output_file = os.path.join(tmp, "sage." + suffix)
# this should get changed if we switch the stuff in misc.viewer to
# producing lists
if debug:
print('viewer: "{}"'.format(viewer))
call('%s %s' % (viewer, output_file), shell=True,
stdout=PIPE, stderr=PIPE)
webbrowser.open(r'file:///{}'.format(output_file))
return


Expand Down
31 changes: 4 additions & 27 deletions src/sage/misc/latex_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
# ****************************************************************************
from subprocess import run
import os
import webbrowser

from sage.structure.sage_object import SageObject
from sage.misc.superseded import experimental
Expand Down Expand Up @@ -733,15 +734,7 @@ def pdf(self, filename=None, view=True, program=None):

# open the tmp pdf
elif view:
from sage.misc.viewer import pdf_viewer
cmd = pdf_viewer().split()
cmd.append(temp_filename_pdf)
# we use check_call as opposed to run, because
# it gives the sage prompt back to the user
# see https://stackoverflow.com/a/71342967
# run(cmd, cwd=base, capture_output=True, check=True)
from subprocess import check_call, PIPE
check_call(cmd, cwd=base, stdout=PIPE, stderr=PIPE)
webbrowser.open(r'file:///{}'.format(temp_filename_pdf))

return temp_filename_pdf

Expand Down Expand Up @@ -821,15 +814,7 @@ def png(self, filename=None, density=150, view=True):

# open the tmp png
elif view:
from sage.misc.viewer import png_viewer
cmd = png_viewer().split()
cmd.append(temp_filename_png)
# we use check_call as opposed to run, because
# it gives the sage prompt back to the user
# see https://stackoverflow.com/a/71342967
# run(cmd, capture_output=True, check=True)
from subprocess import check_call, PIPE
check_call(cmd, stdout=PIPE, stderr=PIPE)
webbrowser.open(r'file:///{}'.format(temp_filename_png))

return temp_filename_png

Expand Down Expand Up @@ -920,15 +905,7 @@ def svg(self, filename=None, view=True, program='pdftocairo'):

# open the tmp svg
elif view:
from sage.misc.viewer import browser
cmd = browser().split()
cmd.append(temp_filename_svg)
# we use check_call as opposed to run, because
# it gives the sage prompt back to the user
# see https://stackoverflow.com/a/71342967
# run(cmd, capture_output=True, check=True)
from subprocess import check_call, PIPE
check_call(cmd, stdout=PIPE, stderr=PIPE)
webbrowser.open(r'file:///{}'.format(temp_filename_svg))

return temp_filename_svg

Expand Down
6 changes: 3 additions & 3 deletions src/sage/misc/sagedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import re
import sys
import pydoc
import webbrowser
from sage.misc.temporary_file import tmp_dir
from .viewer import browser
import sage.version
from sage.env import SAGE_DOC, SAGE_SRC

Expand Down Expand Up @@ -1508,7 +1508,7 @@ def __call__(self, obj, output='html', view=True):

filed.write(html)
filed.close()
os.system(browser() + " " + path)
webbrowser.open(r'file:///{}'.format(path))
else:
return html
elif output == 'rst':
Expand Down Expand Up @@ -1553,7 +1553,7 @@ def _open(self, name, testing=False):
if testing:
return (url, path)

os.system(browser() + " " + path)
webbrowser.open(r'file:///{}'.format(path))

def tutorial(self):
"""
Expand Down
63 changes: 63 additions & 0 deletions src/sage/misc/viewer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
r"""
Determination of programs for viewing web pages, etc.
This module is deprecated: see :trac:`33931`.
The function :func:`default_viewer` defines reasonable defaults for
these programs. To use something else, use ``viewer``. First
import it::
sage: from sage.misc.viewer import viewer
doctest:warning
...
DeprecationWarning: the module sage.misc.viewer is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
On OS X, PDFs are opened by default using the 'open' command, which
runs whatever has been designated as the PDF viewer in the OS. To
Expand All @@ -26,7 +32,10 @@
"""

from sage.structure.sage_object import SageObject
from sage.misc.superseded import deprecation

deprecation(33931, "the module sage.misc.viewer is deprecated; "
"use Python's webbrowser module instead")

VIEWERS = ['browser', 'dvi_viewer', 'pdf_viewer', 'png_viewer']

Expand All @@ -52,6 +61,9 @@ def default_viewer(viewer=None):
...
ValueError: Unknown type of viewer: jpg.
"""
deprecation(33931, "this function is deprecated; "
"use Python's webbrowser module instead")

import os
from sage.misc.sage_ostools import have_program

Expand Down Expand Up @@ -147,6 +159,10 @@ class Viewer(SageObject):
sage: from sage.misc.viewer import viewer
sage: old_browser = viewer.browser() # indirect doctest
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.browser('open -a /Applications/Firefox.app')
sage: viewer.browser()
'open -a /Applications/Firefox.app'
Expand All @@ -167,6 +183,10 @@ def _set(self, app=None, TYPE='browser'):
sage: from sage.misc.viewer import viewer
sage: old_browser = viewer.browser()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.browser('open -a /Applications/Firefox.app') # indirect doctest
sage: viewer.browser()
'open -a /Applications/Firefox.app'
Expand Down Expand Up @@ -201,11 +221,18 @@ def browser(self, app=None):
sage: from sage.misc.viewer import viewer
sage: old_browser = viewer.browser()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.browser('open -a /Applications/Firefox.app') # indirect doctest
sage: viewer.browser()
'open -a /Applications/Firefox.app'
sage: viewer.browser(old_browser) # restore old value
"""
deprecation(33931, "this function is deprecated; "
"use Python's webbrowser module instead")

return self._set(app, TYPE='browser')

def dvi_viewer(self, app=None):
Expand All @@ -221,11 +248,17 @@ def dvi_viewer(self, app=None):
sage: from sage.misc.viewer import viewer
sage: old_dvi_app = viewer.dvi_viewer()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.dvi_viewer('/usr/bin/xdvi') # indirect doctest
sage: viewer.dvi_viewer()
'/usr/bin/xdvi'
sage: viewer.dvi_viewer(old_dvi_app) # restore old value
"""
deprecation(33931, "this function is deprecated; "
"use Python's webbrowser module instead")
return self._set(app, TYPE='dvi_viewer')

def pdf_viewer(self, app=None):
Expand All @@ -241,11 +274,17 @@ def pdf_viewer(self, app=None):
sage: from sage.misc.viewer import viewer
sage: old_pdf_app = viewer.pdf_viewer()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.pdf_viewer('/usr/bin/pdfopen') # indirect doctest
sage: viewer.pdf_viewer()
'/usr/bin/pdfopen'
sage: viewer.pdf_viewer(old_pdf_app) # restore old value
"""
deprecation(33931, "this function is deprecated; "
"use Python's webbrowser module instead")
return self._set(app, TYPE='pdf_viewer')

def png_viewer(self, app=None):
Expand All @@ -261,11 +300,17 @@ def png_viewer(self, app=None):
sage: from sage.misc.viewer import viewer
sage: old_png_app = viewer.png_viewer()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.png_viewer('display') # indirect doctest
sage: viewer.png_viewer()
'display'
sage: viewer.png_viewer(old_png_app) # restore old value
"""
deprecation(33931, "this function is deprecated; "
"use Python's webbrowser module instead")
return self._set(app, TYPE='png_viewer')

def __call__(self, x=None):
Expand All @@ -287,8 +332,14 @@ def __call__(self, x=None):
sage: viewer('pdf') # random -- depends on OS, etc.
'mozilla'
sage: viewer('browser') == viewer()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
True
"""
deprecation(33931, "this function is deprecated; "
"use Python's webbrowser module instead")
if isinstance(x, str):
x = x.lower()

Expand Down Expand Up @@ -348,8 +399,16 @@ def pdf_viewer():
sage: from sage.misc.viewer import pdf_viewer, viewer
sage: old_pdf_app = viewer.pdf_viewer()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
sage: viewer.pdf_viewer('acroread')
sage: pdf_viewer()
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
'acroread'
sage: viewer.pdf_viewer('old_pdf_app')
"""
Expand All @@ -368,6 +427,10 @@ def png_viewer():
sage: from sage.misc.viewer import png_viewer
sage: png_viewer() # random -- depends on OS, etc.
doctest:warning
...
DeprecationWarning: this function is deprecated; use Python's webbrowser module instead
See https://trac.sagemath.org/33931 for details.
'xdg-open'
"""
viewer()
Expand Down
1 change: 0 additions & 1 deletion src/sage/plot/animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
from sage.misc.temporary_file import tmp_dir, tmp_filename
from . import plot
import sage.misc.misc
import sage.misc.viewer


def animate(frames, **kwds):
Expand Down
8 changes: 2 additions & 6 deletions src/sage/repl/rich_output/backend_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import os
import sys
import webbrowser
from IPython.display import publish_display_data
from sage.repl.rich_output.backend_base import BackendBase
from sage.repl.rich_output.output_catalog import *
Expand Down Expand Up @@ -331,14 +332,9 @@ def launch_viewer(self, image_file, plain_text):
"""
base, dot_ext = os.path.splitext(image_file)
ext = dot_ext.lstrip(os.path.extsep)
from sage.misc.viewer import viewer
command = viewer(ext)
if not command:
command = viewer.browser()
from sage.doctest import DOCTEST_MODE
if not DOCTEST_MODE:
os.system('{0} {1} 2>/dev/null 1>/dev/null &'
.format(command, image_file))
webbrowser.open(r'file:///{}'.format(image_file))
return 'Launched {0} viewer for {1}'.format(ext, plain_text)

def launch_jmol(self, output_jmol, plain_text):
Expand Down

0 comments on commit bbe8110

Please sign in to comment.