Skip to content

Commit

Permalink
Mini browser window for #33
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzoic committed Mar 27, 2024
1 parent 8155ebd commit bd92dca
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
16 changes: 14 additions & 2 deletions countess/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import threading
import tkinter as tk
import webbrowser
from tkinter import filedialog, messagebox, ttk
from typing import Optional

Expand All @@ -15,6 +14,7 @@
from countess.core.plugins import get_plugin_classes
from countess.gui.config import PluginConfigurator
from countess.gui.logger import LoggerFrame
from countess.gui.mini_browser import MiniBrowserFrame
from countess.gui.tabular import TabularDataFrame
from countess.gui.tree import FlippyCanvas, GraphWrapper
from countess.gui.widgets import info_button
Expand Down Expand Up @@ -57,6 +57,8 @@ class ConfiguratorWrapper:
config_change_task = None
notes_widget = None
node_update_thread = None
info_toplevel = None
info_frame = None

def __init__(self, frame, node, change_callback):
self.frame = frame
Expand Down Expand Up @@ -138,7 +140,17 @@ def on_label_configure(self, *_):
self.label["wraplength"] = self.label.winfo_width() - 20

def on_info_button_press(self, *_):
webbrowser.open_new_tab(self.node.plugin.link)
if self.info_toplevel is None:
self.info_toplevel = tk.Toplevel()
self.info_toplevel.protocol("WM_DELETE_WINDOW", self.on_info_toplevel_close)
self.info_frame = MiniBrowserFrame(self.info_toplevel, self.node.plugin.link)
self.info_frame.pack(fill="both", expand=True)
else:
self.info_frame.load_url(self.node.plugin.link)

def on_info_toplevel_close(self):
self.info_toplevel.destroy()
self.info_toplevel = None

def on_add_notes(self, *_):
self.notes_widget.destroy()
Expand Down
54 changes: 54 additions & 0 deletions countess/gui/mini_browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import tkinter as tk
from urllib.parse import urlparse
import webbrowser

from tkinterweb import HtmlFrame #import the HTML browser

MINI_CSS = """
* { padding: 10px; line-height: 150% }
th, td { border: 1px solid #AAA; border-collapse: collapse; }
code { border: 1px solid #AAA; font-family: monospace; }
th code, td code { border: 0px; }
"""

class MiniBrowserFrame(tk.Frame):

def __init__(self, tk_parent, start_url, *a, **k):
super().__init__(tk_parent, *a, **k)
tk.Label(self, text="Documentation Preview").pack(fill="both")

self.html_frame = HtmlFrame(self, messages_enabled=False)
self.html_frame.enable_stylesheets(False)
self.html_frame.enable_objects(False)
self.html_frame.enable_forms(False)
self.html_frame.on_done_loading(self.on_done_loading)
self.html_frame.on_link_click(self.on_link_click)

self.html_frame.pack(fill="both", expand=True)

tk.Button(self, text="Open in Browser", command=self.on_browser_button).pack()

self.load_url(start_url)

def load_url(self, link_url):
self.current_url = link_url
self.html_frame.load_url(link_url)

def on_browser_button(self):
webbrowser.open_new_tab(self.current_url)

def on_done_loading(self):
self.html_frame.add_css(MINI_CSS)

def on_link_click(self, link_url):
if urlparse(self.current_url)[0:2] == urlparse(link_url)[0:2]:
self.load_url(link_url)
else:
webbrowser.open_new_tab(link_url)


if __name__ == '__main__':
root = tk.Tk()
url = "https://countess-project.github.io/CountESS/"
MiniBrowserFrame(root, url).pack(fill="both", expand=True)
root.mainloop()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
'pandas~=2.1.0',
'psutil~=5.9.5',
'rapidfuzz~=2.15.1',
'tkinterweb~=3.23.5',
'ttkthemes~=3.2.2',
'typing_extensions~=4.8.0',
]
Expand Down

0 comments on commit bd92dca

Please sign in to comment.