Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

import application 

import pathlib 

import sys 

import PyQt5.QtGui 

import PyQt5.QtWidgets 

import PyQt5.QtWebEngineWidgets 

import PyQt5.QtCore 

 

 

parent_dir = pathlib.Path(__file__).parent 

TITLE = "Topics Explorer" 

ICON = str(pathlib.Path(parent_dir, "static", "img", "app_icon.png")) 

PORT = 5000 

 

if hasattr(PyQt5.QtCore.Qt, "AA_EnableHighDpiScaling"): 

PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_EnableHighDpiScaling, True) 

 

if hasattr(PyQt5.QtCore.Qt, "AA_UseHighDpiPixmaps"): 

PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_UseHighDpiPixmaps, True) 

 

def download_requested(item): 

""" 

Opens a file dialog to save the ZIP archive. 

""" 

path = PyQt5.QtWidgets.QFileDialog.getSaveFileName(None, 

"Select destination folder and file name", 

"", 

"Zip files (*.zip)")[0] 

item.setPath("{path}.{ext}".format(path=path, ext="zip")) 

item.accept() 

 

class ApplicationThread(PyQt5.QtCore.QThread): 

def __init__(self, application, port=PORT): 

super(ApplicationThread, self).__init__() 

self.application = application 

self.port = port 

 

def __del__(self): 

self.wait() 

 

def run(self): 

self.application.run(port=self.port, threaded=True) 

 

""" 

# This part allows you to open external links in the standard browser, 

# but has been discarded because potential dead links should be avoided 

# in the application. 

 

class WebPage(PyQt5.QtWebEngineWidgets.QWebEnginePage): 

def __init__(self, root_url): 

super(WebPage, self).__init__() 

self.root_url = root_url 

 

def home(self): 

self.load(PyQt5.QtCore.QUrl(self.root_url)) 

 

def acceptNavigationRequest(self, url, kind, is_main_frame): 

ready_url = url.toEncoded().data().decode() 

is_clicked = kind == self.NavigationTypeLinkClicked 

 

if is_clicked and (self.root_url not in ready_url): 

PyQt5.QtGui.QDesktopServices.openUrl(url) 

return False 

return super(WebPage, self).acceptNavigationRequest(url, kind, is_main_frame) 

""" 

 

def init_gui(flask_app, port=PORT, argv=None, title=TITLE, icon=ICON): 

""" 

Initializes the Qt web engine, starts the web application, and loads the 

main page. 

""" 

if argv is None: 

argv = sys.argv 

 

# Starting the Flask application. 

qtapp = PyQt5.QtWidgets.QApplication(argv) 

webapp = ApplicationThread(flask_app, port) 

webapp.start() 

 

def cleanup(webapp=webapp): 

""" 

Killing the Flask process and removing temporary 

folders after user closed the window. 

""" 

webapp.terminate() 

dumpdir, archivedir = application.utils.get_tempdirs() 

application.utils.unlink_content(dumpdir) 

application.utils.unlink_content(archivedir) 

dumpdir.rmdir() 

archivedir.rmdir() 

 

qtapp.aboutToQuit.connect(cleanup) 

 

# Setting width and height individually based on the  

# screen resolution: 93% of the screen for width, 

# 80% for height. 

screen = qtapp.primaryScreen() 

size = screen.size() 

width = size.width() - size.width() / 100 * 7 

height = size.height() - size.height() / 100 * 20 

 

# Applying settings and loading the main page. 

webview = PyQt5.QtWebEngineWidgets.QWebEngineView() 

webview.resize(width, height) 

webview.setWindowTitle(title) 

webview.setWindowIcon(PyQt5.QtGui.QIcon(icon)) 

webview.load(PyQt5.QtCore.QUrl("http://localhost:{}".format(port))) 

 

# If the user clicks a download button, a window pops up. 

webview.page().profile().downloadRequested.connect(download_requested) 

 

# Show the webview. 

webview.show() 

return qtapp.exec_() 

 

def run(): 

""" 

Calls the main function. 

""" 

sys.exit(init_gui(application.web.app))