This repository was archived by the owner on Jun 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (77 loc) · 3.14 KB
/
main.py
File metadata and controls
113 lines (77 loc) · 3.14 KB
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
"""Main Python application file for the EEL-CRA demo.
To build for production:
npm run build
python -m eel main.py dist_vite --onefile --splash splashfile.png --path env/lib/site-packages --noconsole
"""
import os
import platform
import random
import sys
import importlib
import socket, errno
from py_src.contrib.replace_in_file import replaceInfile, findFileRe
from py_src.contrib.port_check import find_unused_port
import eel
if '_PYIBoot_SPLASH' in os.environ and importlib.util.find_spec("pyi_splash"):
import pyi_splash
pyi_splash.update_text('UI Loaded ...')
pyi_splash.close()
@eel.expose # Expose function to JavaScript
def say_hello_py(x):
"""Print message from JavaScript on app initialization, then call a JS function."""
print('Hello from %s' % x) # noqa T001
eel.say_hello_js('Python {from within say_hello_py()}!')
@eel.expose
def expand_user(folder):
"""Return the full path to display in the UI."""
return '{}/*'.format(os.path.expanduser(folder))
@eel.expose
def pick_file(folder):
"""Return a random file from the specified folder."""
folder = os.path.expanduser(folder)
if os.path.isdir(folder):
listFiles = [_f for _f in os.listdir(folder) if not os.path.isdir(os.path.join(folder, _f))]
if len(listFiles) == 0:
return 'No Files found in {}'.format(folder)
return random.choice(listFiles)
else:
return '{} is not a valid folder'.format(folder)
def start_eel(develop):
"""Start Eel with either production or development configuration."""
if develop:
directory = 'web_src'
app = None
page = {'port': 5173}
eel_port = 5169
else:
directory = 'dist_vite'
app = 'chrome'
page = 'index.html'
# find a unused port to host the eel server/websocket
eel_port = find_unused_port()
# replace the port in the web files
replace_file = findFileRe("./dist_vite/assets", "index.*.js")
replaceInfile(f"./dist_vite/assets/{replace_file}", 'ws://localhost:....', f"ws://localhost:{eel_port}")
replaceInfile("./dist_vite/index.html", 'http://localhost:.....eel.js', f"http://localhost:{eel_port}/eel.js")
eel.init(directory, ['.tsx', '.ts', '.jsx', '.js', '.html'])
# These will be queued until the first connection is made, but won't be repeated on a page reload
say_hello_py('Python World!')
eel.say_hello_js('Python World!') # Call a JavaScript function (must be after `eel.init()`)
eel.show_log('https://github.com/samuelhwilliams/Eel/issues/363 (show_log)')
eel_kwargs = dict(
host='localhost',
port=eel_port,
size=(1280, 800),
)
try:
eel.start(page, mode=app, **eel_kwargs)
except EnvironmentError:
# If Chrome isn't found, fallback to Microsoft Edge on Win10 or greater
if sys.platform in ['win32', 'win64'] and int(platform.release()) >= 10:
eel.start(page, mode='edge', **eel_kwargs)
else:
raise
if __name__ == '__main__':
import sys
# Pass any second argument to enable debugging
start_eel(develop=len(sys.argv) == 2)