Skip to content

Commit

Permalink
Merging PR #74 (jinja2 templates)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKnott committed Feb 13, 2019
1 parent da99427 commit 3dc7e75
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ build
Eel.egg-info
.tmp
.DS_Store
*.pyc
*.pyc
*.swp
venv/
24 changes: 18 additions & 6 deletions eel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
'chromeFlags': []
}


# Public functions

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


def start(*start_urls, **kwargs):
global _on_close_callback

global _on_close_callback, _jinja_env, _jinja_templates
block = kwargs.pop('block', True)
_jinja_templates = kwargs.pop('templates', None)
options = kwargs.pop('options', {})
size = kwargs.pop('size', None)
position = kwargs.pop('position', None)
Expand All @@ -110,6 +109,14 @@ def start(*start_urls, **kwargs):
options['port'] = sock.getsockname()[1]
sock.close()

if _jinja_templates != None:
from jinja2 import Environment, FileSystemLoader, select_autoescape
templates_path = os.path.join(root_path, _jinja_templates)
_jinja_env = Environment(loader=FileSystemLoader(templates_path),
autoescape=select_autoescape(['html', 'xml']))
else:
_jinja_env = None

brw.open(start_urls, options)

def run_lambda():
Expand Down Expand Up @@ -146,13 +153,18 @@ def _eel():

@btl.route('/<path:path>')
def _static(path):
return btl.static_file(path, root=root_path)

if _jinja_env != None:
n = len(_jinja_templates + '/')
template = _jinja_env.get_template(path[n:])
return template.render()
else:
return btl.static_file(path, root=root_path)


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

for js_function in _js_functions:
_import_js_function(js_function)
Expand Down
12 changes: 12 additions & 0 deletions examples/06 - jinja_templates/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import eel

eel.init('web/') # Give folder containing web files

@eel.expose # Expose this function to Javascript
def say_hello_py(x):
print('Hello from %s' % x)

say_hello_py('Python World!')
eel.say_hello_js('Python World!') # Call a Javascript function

eel.start('templates/hello.html', size=(300, 200), templates=True) # Start
Binary file added examples/06 - jinja_templates/web/favicon.ico
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/06 - jinja_templates/web/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript">
{% block head_scripts %}{% endblock %}
</script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
27 changes: 27 additions & 0 deletions examples/06 - jinja_templates/web/templates/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends 'base.html' %}
{% block title %}Hello, World!{% endblock %}
{% block head_scripts %}
eel.expose(say_hello_js); // Expose this function to Python
function say_hello_js(x) {
console.log("Hello from " + x);
}

eel.expose(js_random);
function js_random() {
return Math.random();
}

function print_num(n) {
console.log('Got this from Python: ' + n);
}

eel.py_random()(print_num);

say_hello_js("Javascript World!");
eel.say_hello_py("Javascript World!"); // Call a Python function
{% endblock %}
{% block content %}
Hello, World!
<br />
<a href="page2.html">Page 2</a>
{% endblock %}
5 changes: 5 additions & 0 deletions examples/06 - jinja_templates/web/templates/page2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends 'base.html' %}
{% block title %}Hello, World!{% endblock %}
{% block content %}
<h1>This is page 2</h1>
{% endblock %}
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bottle==0.12.13
bottle-websocket==0.2.9
gevent==1.3.6
gevent-websocket==0.10.1
greenlet==0.4.15
pkg-resources==0.0.0
whichcraft==0.4.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='Eel',
version='0.9.12',
version='0.9.13',
author='Chris Knott',
author_email='[email protected]',
packages=['eel'],
Expand Down

0 comments on commit 3dc7e75

Please sign in to comment.