Skip to content

Commit 3dc7e75

Browse files
committed
Merging PR python-eel#74 (jinja2 templates)
1 parent da99427 commit 3dc7e75

9 files changed

Lines changed: 87 additions & 8 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ build
44
Eel.egg-info
55
.tmp
66
.DS_Store
7-
*.pyc
7+
*.pyc
8+
*.swp
9+
venv/

eel/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
'chromeFlags': []
3434
}
3535

36-
3736
# Public functions
3837

3938
def expose(name_or_function=None):
@@ -88,9 +87,9 @@ def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm', '.xhtml']):
8887

8988

9089
def start(*start_urls, **kwargs):
91-
global _on_close_callback
92-
90+
global _on_close_callback, _jinja_env, _jinja_templates
9391
block = kwargs.pop('block', True)
92+
_jinja_templates = kwargs.pop('templates', None)
9493
options = kwargs.pop('options', {})
9594
size = kwargs.pop('size', None)
9695
position = kwargs.pop('position', None)
@@ -110,6 +109,14 @@ def start(*start_urls, **kwargs):
110109
options['port'] = sock.getsockname()[1]
111110
sock.close()
112111

112+
if _jinja_templates != None:
113+
from jinja2 import Environment, FileSystemLoader, select_autoescape
114+
templates_path = os.path.join(root_path, _jinja_templates)
115+
_jinja_env = Environment(loader=FileSystemLoader(templates_path),
116+
autoescape=select_autoescape(['html', 'xml']))
117+
else:
118+
_jinja_env = None
119+
113120
brw.open(start_urls, options)
114121

115122
def run_lambda():
@@ -146,13 +153,18 @@ def _eel():
146153

147154
@btl.route('/<path:path>')
148155
def _static(path):
149-
return btl.static_file(path, root=root_path)
150-
156+
if _jinja_env != None:
157+
n = len(_jinja_templates + '/')
158+
template = _jinja_env.get_template(path[n:])
159+
return template.render()
160+
else:
161+
return btl.static_file(path, root=root_path)
162+
151163

152164
@btl.get('/eel', apply=[wbs.websocket])
153165
def _websocket(ws):
154166
global _websockets
155-
global _message_loop_queue
167+
global _message_loop_queue # <- what is this for...?
156168

157169
for js_function in _js_functions:
158170
_import_js_function(js_function)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import eel
2+
3+
eel.init('web/') # Give folder containing web files
4+
5+
@eel.expose # Expose this function to Javascript
6+
def say_hello_py(x):
7+
print('Hello from %s' % x)
8+
9+
say_hello_py('Python World!')
10+
eel.say_hello_js('Python World!') # Call a Javascript function
11+
12+
eel.start('templates/hello.html', size=(300, 200), templates=True) # Start
14.7 KB
Binary file not shown.
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>{% block title %}{% endblock %}</title>
5+
<!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
6+
<script type="text/javascript" src="/eel.js"></script>
7+
<script type="text/javascript">
8+
{% block head_scripts %}{% endblock %}
9+
</script>
10+
</head>
11+
<body>
12+
{% block content %}{% endblock %}
13+
</body>
14+
</html>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% extends 'base.html' %}
2+
{% block title %}Hello, World!{% endblock %}
3+
{% block head_scripts %}
4+
eel.expose(say_hello_js); // Expose this function to Python
5+
function say_hello_js(x) {
6+
console.log("Hello from " + x);
7+
}
8+
9+
eel.expose(js_random);
10+
function js_random() {
11+
return Math.random();
12+
}
13+
14+
function print_num(n) {
15+
console.log('Got this from Python: ' + n);
16+
}
17+
18+
eel.py_random()(print_num);
19+
20+
say_hello_js("Javascript World!");
21+
eel.say_hello_py("Javascript World!"); // Call a Python function
22+
{% endblock %}
23+
{% block content %}
24+
Hello, World!
25+
<br />
26+
<a href="page2.html">Page 2</a>
27+
{% endblock %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends 'base.html' %}
2+
{% block title %}Hello, World!{% endblock %}
3+
{% block content %}
4+
<h1>This is page 2</h1>
5+
{% endblock %}

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bottle==0.12.13
2+
bottle-websocket==0.2.9
3+
gevent==1.3.6
4+
gevent-websocket==0.10.1
5+
greenlet==0.4.15
6+
pkg-resources==0.0.0
7+
whichcraft==0.4.1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='Eel',
6-
version='0.9.12',
6+
version='0.9.13',
77
author='Chris Knott',
88
author_email='chrisknott@hotmail.co.uk',
99
packages=['eel'],

0 commit comments

Comments
 (0)