Skip to content

Commit

Permalink
Clean up imports in tkterminal; fix backslash errors; fix process_ret…
Browse files Browse the repository at this point in the history
…urn.
  • Loading branch information
culler committed Oct 25, 2024
1 parent 6eb2e34 commit bb3f498
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 51 deletions.
40 changes: 35 additions & 5 deletions python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .phone_home import update_needed
from .SnapPy import SnapPea_interrupt, msg_stream
from .shell import SnapPyInteractiveShellEmbed
from .tkterminal import TkTerm, snappy_path
from .tkterminal import TkTerminalBase

from plink import LinkEditor
from plink.smooth import Smoother
Expand All @@ -34,7 +34,7 @@
os.environ['HOME'] = os.environ['SNAPPYHOME']


class SnapPyTerm(TkTerm, ListedWindow):
class SnapPyTerm(TkTerminalBase, ListedWindow):
"""
The main window of the SnapPy app, which runs an embedded IPython shell.
"""
Expand All @@ -47,9 +47,10 @@ def __init__(self):
self.main_window = self
self.menu_title = 'SnapPy Shell'
self.register_window(self)
TkTerm.__init__(self, shell, name='SnapPy Command Shell')
TkTerminalBase.__init__(self, shell, name='SnapPy Command Shell')
self.settings = SnapPySettings(self)
self.start_interaction()
self.interact_prompt()
if sys.platform == 'darwin':
assert str(self.window) == "."
# Under OS X, the main window shouldn't be closable.
Expand All @@ -61,6 +62,34 @@ def __init__(self):
self.window.tk.call('set', '::tk::dialog::file::showHiddenVar', '0')
self.encoding = None

def start_interaction(self):
"""
Display a banner and prepare to begin interaction.
"""
snappy_path = os.path.abspath(os.path.dirname(__file__))
icon_file = os.path.join(snappy_path, 'info_icon.gif')
# Keep a reference to the icon.
self.icon = Tk_.PhotoImage(file=icon_file)

self.text.image_create(Tk_.END, image=self.icon)
banner_label = Tk_.Label(self.text, text=self.banner,
background='#ec0fffec0',
foreground='DarkGreen',
anchor=Tk_.W,
justify=Tk_.LEFT,
font=self.settings['font'])
self.text.window_create(Tk_.END, window=banner_label)
self.text.insert(Tk_.END, '\n')
self.text.mark_set('output_end', '2.0')
# Set a reasonable default directory for files to be saved to.
try:
home = os.environ['HOME']
except KeyError:
home = os.path.expanduser("~")
desktop = os.path.join(home, "Desktop")
default_save_dir = desktop if os.path.exists(desktop) else home
self.IP.magics_manager.magics['line']['cd']("-q " + default_save_dir)

def add_bindings(self):
self.window.bind('<<Paste>>', self.edit_paste)

Expand Down Expand Up @@ -480,18 +509,19 @@ def set_icon(window):
if sys.platform == 'win32':
try:
import snappy
ico = os.path.join(os.path.dirname(snappy.__file__), 'SnapPy.ico')
ico = os.path.join(os.path.dirname(__file__), 'SnapPy.ico')
window.iconbitmap(default=ico)
except:
pass
if sys.platform == 'darwin':
if not sys.executable.endswith('SnapPy.app/Contents/MacOS/python'):
snappy_path = os.path.abspath(os.path.dirname(__file__))
icon_file = os.path.join(snappy_path, 'info_icon.gif')
image_file = os.path.join(snappy_path, 'SnapPy.png')
if os.path.exists(image_file):
dock_icon = Tk_.PhotoImage(file=image_file)
window.eval('wm iconphoto . -default %s' % dock_icon)


# from multiprocessing import Process
class SnapPyKernelServer():
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def _move_lifted_vertex_positions_cusp(cusp : t3m.Vertex):
}

def scale_triangles_to_avoid_standard_tubes(self):
"""
r"""
Scales each horo triangle so that it is guaranteed to be outside of
a standard tube about the incompleteness locus from the outside (up
to rounding errors, it will touch the tube from the outside). Note
Expand Down
2 changes: 1 addition & 1 deletion python/raytracing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Sequence

def inside_view(self, cohomology_class=None, geodesics : Sequence[str]=[]):
"""
r"""
Show raytraced inside view of hyperbolic manifold. See
`images <https://im.icerm.brown.edu/portfolio/snappy-views/>`_
and `demo video <https://youtu.be/CAERhmUCkRs>`_.
Expand Down
65 changes: 21 additions & 44 deletions python/tkterminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import sys
import re
from urllib.request import pathname2url

from IPython.utils import io
from IPython.core.autocall import IPyAutocall

import snappy
from .gui import *
import tkinter as Tk_
from tkinter import ttk
from tkinter.font import Font
from tkinter.messagebox import askyesno
from IPython.utils import io

snappy_path = os.path.abspath(os.path.dirname(snappy.__file__))
icon_file = os.path.join(snappy_path, 'info_icon.gif')
debug_Tk = True
ansi_seqs = re.compile(r'(?:\x01*\x1b\[((?:[0-9]*;)*[0-9]*.)\x02*)*([^\x01\x1b]*)',
re.MULTILINE)
Expand Down Expand Up @@ -42,15 +38,8 @@ def __init__(self, error_handler=None):
# calls this function to report their occurrence.
if error_handler:
self.report_callback_exception = error_handler
# In Python 2.7 the _default root does not get set correctly.
if not Tk_._default_root:
Tk_._default_root = self

# Some ideas for the TkTerm class were borrowed from code written by
# Eitan Isaacson, IBM Corp.


class TkTerm:
class TkTerminalBase:
"""
A Tkinter terminal window that runs an IPython shell. This class
supports the IOStream interface, and can function as a replacement
Expand All @@ -62,16 +51,13 @@ def __init__(self, shell, name='TkTerm'):
io.stdout = sys.stdout = self
else:
self.window = window = Tk(self.report_callback_exception)
# self.encoding = sys.stdout.encoding
# self.saved_io = (sys.stdout, sys.stderr)
io.stdout = io.stderr = sys.stdout = sys.stderr = self
self._input_buffer = ''
self._current_indent = 0
# Global Tk options
window.option_add('*Menu.tearOff', 0)
window.title(name)
window.protocol("WM_DELETE_WINDOW", self.close)
self.icon = Tk_.PhotoImage(file=icon_file)
self.frame = frame = Tk_.Frame(window)
self.text = text = Tk_.Text(frame,
width=85,
Expand Down Expand Up @@ -377,17 +363,20 @@ def handle_shift_return(self, event):
return self.handle_return()

def process_return(self, cell):
try:
self.interact_handle_input(cell)
except KeyboardInterrupt:
self.write('(IP) Keyboard Interrupt: ')
self.reset()
line, pos = map(int, self.text.index(Tk_.INSERT).split('.'))
last_line = int(self.text.index(Tk_.END).split('.')[0])
if line == last_line - 1:
try:
self.interact_handle_input(cell)
except KeyboardInterrupt:
self.write('(IP) Keyboard Interrupt: ')
self.reset()
self.hist_pointer = 0
self.hist_stem = ''
self.interact_prompt()
self.text.see(Tk_.INSERT)
if self.IP.more:
self.text.insert(Tk_.INSERT, ' '*self._current_indent, ())
self.hist_pointer = 0
self.hist_stem = ''

def jump_up(self, event):
return self.handle_up(event, jump=True)
Expand Down Expand Up @@ -673,28 +662,16 @@ def middle_mouse_up(self, event):

def start_interaction(self):
"""
Print the banner and issue the first prompt.
Display a banner and prepare to begin interaction.
"""
self.text.image_create(Tk_.END, image=self.icon)
banner_label = Tk_.Label(self.text, text=self.banner,
background='#ec0fffec0',
foreground='DarkGreen',
anchor=Tk_.W,
justify=Tk_.LEFT,
font=self.settings['font'])
# Subclasses should override this method
banner_label = Tk_.Label(self.text,
text="Please override the start_interaction method.",
anchor=Tk_.W,
justify=Tk_.LEFT)
self.text.window_create(Tk_.END, window=banner_label)
self.text.insert(Tk_.END, '\n')
self.text.mark_set('output_end', '2.0')
# Set a reasonable default directory for files to be saved to.
try:
home = os.environ['HOME']
except KeyError:
home = os.path.expanduser("~")
desktop = os.path.join(home, "Desktop")
default_save_dir = desktop if os.path.exists(desktop) else home
self.IP.magics_manager.magics['line']['cd']("-q " + default_save_dir)
# Create the prompt and go!
self.interact_prompt()

def _input_prompt(self):
result = [('Prompt', 'In['),
Expand Down

0 comments on commit bb3f498

Please sign in to comment.