Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 39759bf

Browse files
committed
Add examples.
1 parent dbffb4a commit 39759bf

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import eel
2+
import json
3+
import datetime
4+
5+
eel.init('web')
6+
7+
@eel.expose
8+
def py_json_data_sender():
9+
return {
10+
'datetime': datetime.datetime.now()
11+
}
12+
13+
@eel.expose
14+
def py_json_data_loader():
15+
return eel.js_json_data_sender()(print_value)
16+
17+
def print_value(v):
18+
print('Got this value from javascript:')
19+
print(v)
20+
21+
# Custom Json Encoder.
22+
class EelJsonEncoder(json.JSONEncoder):
23+
def default(self, o):
24+
if isinstance(o, datetime.date):
25+
return o.__str__()
26+
27+
28+
# Custom Json Decoder.
29+
class CustomDatetimeObj(object):
30+
def __init__(self, datetime):
31+
self.datetime = datetime
32+
33+
def __str__(self):
34+
return 'CustomDatetimeObj: %s' % self.datetime.__str__()
35+
36+
def decoder_object_hook(o):
37+
if 'datetime' in o:
38+
return CustomDatetimeObj(datetime=o['datetime'])
39+
else:
40+
return o
41+
42+
class EelJsonDecoder(json.JSONDecoder):
43+
def __init__(self, object_hook=decoder_object_hook, *args, **kwargs):
44+
super().__init__(object_hook=object_hook, *args, **kwargs)
45+
46+
47+
eel.start('custom_json_encoder_decoder.html', json_encoder=EelJsonEncoder,
48+
json_decoder=EelJsonDecoder, size=(400, 300))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Custom JSON Encoder/Decoder Eel Example</title>
5+
6+
<!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
7+
<script type="text/javascript" src="/eel.js"></script>
8+
<script type="text/javascript" src="custom_json_encoder_decoder.js"></script>
9+
</head>
10+
11+
<body>
12+
Hello World!
13+
</body>
14+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
eel.expose(js_json_data_sender);
2+
function js_json_data_sender() {
3+
return {
4+
datetime: new Date()
5+
}
6+
}
7+
8+
function print_value(v) {
9+
console.log('Got this value from python:');
10+
console.log(v);
11+
}
12+
13+
// Call Python function.
14+
eel.py_json_data_sender()(print_value);
15+
16+
// Call JS function from python.
17+
eel.py_json_data_loader();
14.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)