Eel
A little Python library for making simple Electron-like HTML/JS GUI apps
Install / Use
/learn @python-eel/EelREADME
Eel
[!CAUTION] This project is effectively unmaintained. It has not received regular update in a number of years, and there are no plans by active maintainers for this to change. Please treat this project in that light and use it with careful consideration towards how you will secure and maintain anything you build using it.
Eel is a little Python library for making simple Electron-like offline HTML/JS GUI apps, with full access to Python capabilities and libraries.
Eel hosts a local webserver, then lets you annotate functions in Python so that they can be called from Javascript, and vice versa.
Eel is designed to take the hassle out of writing short and simple GUI applications. If you are familiar with Python and web development, probably just jump to this example which picks random file names out of the given folder (something that is impossible from a browser).
<p align="center"><img src="https://raw.githubusercontent.com/samuelhwilliams/Eel/master/examples/04%20-%20file_access/Screenshot.png" ></p> <!-- TOC --> <!-- /TOC -->Intro
There are several options for making GUI apps in Python, but if you want to use HTML/JS (in order to use jQueryUI or Bootstrap, for example) then you generally have to write a lot of boilerplate code to communicate from the Client (Javascript) side to the Server (Python) side.
The closest Python equivalent to Electron (to my knowledge) is cefpython. It is a bit heavy weight for what I wanted.
Eel is not as fully-fledged as Electron or cefpython - it is probably not suitable for making full blown applications like Atom - but it is very suitable for making the GUI equivalent of little utility scripts that you use internally in your team.
For some reason many of the best-in-class number crunching and maths libraries are in Python (Tensorflow, Numpy, Scipy etc) but many of the best visualization libraries are in Javascript (D3, THREE.js etc). Hopefully Eel makes it easy to combine these into simple utility apps for assisting your development.
Join Eel's users and maintainers on Discord, if you like.
Install
Install from pypi with pip:
pip install eel
To include support for HTML templating, currently using Jinja2:
pip install eel[jinja2]
Usage
Directory Structure
An Eel application will be split into a frontend consisting of various web-technology files (.html, .js, .css) and a backend consisting of various Python scripts.
All the frontend files should be put in a single directory (they can be further divided into folders inside this if necessary).
my_python_script.py <-- Python scripts
other_python_module.py
static_web_folder/ <-- Web folder
main_page.html
css/
style.css
img/
logo.png
Starting the app
Suppose you put all the frontend files in a directory called web, including your start page main.html, then the app is started like this;
import eel
eel.init('web')
eel.start('main.html')
This will start a webserver on the default settings (http://localhost:8000) and open a browser to http://localhost:8000/main.html.
If Chrome or Chromium is installed then by default it will open in that in App Mode (with the --app cmdline flag), regardless of what the OS's default browser is set to (it is possible to override this behaviour).
App options
Additional options can be passed to eel.start() as keyword arguments.
Some of the options include the mode the app is in (e.g. 'chrome'), the port the app runs on, the host name of the app, and adding additional command line flags.
As of Eel v0.12.0, the following options are available to start():
- mode, a string specifying what browser to use (e.g.
'chrome','electron','edge','msie','custom'). Can also beNoneorFalseto not open a window. Default:'chrome' - host, a string specifying what hostname to use for the Bottle server. Default:
'localhost') - port, an int specifying what port to use for the Bottle server. Use
0for port to be picked automatically. Default:8000. - block, a bool saying whether or not the call to
start()should block the calling thread. Default:True - jinja_templates, a string specifying a folder to use for Jinja2 templates, e.g.
my_templates. Default:None - cmdline_args, a list of strings to pass to the command to start the browser. For example, we might add extra flags for Chrome;
eel.start('main.html', mode='chrome-app', port=8080, cmdline_args=['--start-fullscreen', '--browser-startup-dialog']). Default:[] - size, a tuple of ints specifying the (width, height) of the main window in pixels Default:
None - position, a tuple of ints specifying the (left, top) of the main window in pixels Default:
None - geometry, a dictionary specifying the size and position for all windows. The keys should be the relative path of the page, and the values should be a dictionary of the form
{'size': (200, 100), 'position': (300, 50)}. Default: {} - close_callback, a lambda or function that is called when a websocket to a window closes (i.e. when the user closes the window). It should take two arguments; a string which is the relative path of the page that just closed, and a list of other websockets that are still open. Default:
None - app, an instance of Bottle which will be used rather than creating a fresh one. This can be used to install middleware on the instance before starting eel, e.g. for session management, authentication, etc. If your
appis not a Bottle instance, you will need to calleel.register_eel_routes(app)on your custom app instance. - shutdown_delay, timer configurable for Eel's shutdown detection mechanism, whereby when any websocket closes, it waits
shutdown_delayseconds, and then checks if there are now any websocket connections. If not, then Eel closes. In case the user has closed the browser and wants to exit the program. By default, the value of shutdown_delay is1.0second
Exposing functions
In addition to the files in the frontend folder, a Javascript library will be served at /eel.js. You should include this in any pages:
<script type="text/javascript" src="/eel.js"></script>
Including this library creates an eel object which can be used to communicate with the Python side.
Any functions in the Python code which are decorated with @eel.expose like this...
@eel.expose
def my_python_function(a, b):
print(a, b, a + b)
...will appear as methods on the eel object on the Javascript side, like this...
console.log("Calling Python...");
eel.my_python_function(1, 2); // This calls the Python function that was decorated
Similarly, any Javascript functions which are exposed like this...
eel.expose(my_javascript_function);
function my_javascript_function(a, b, c, d) {
if (a < b) {
console.log(c * d);
}
}
can be called from the Python side like this...
print('Calling Javascript...')
eel.my_javascript_function(1, 2, 3, 4) # This calls the Javascript function
The exposed name can also be overridden by passing in a second argument. If your app minifies JavaScript during builds, this may be necessary to ensure that functions can be resolved on the Python side:
eel.expose(someFunction, "my_javascript_function");
When passing complex objects as arguments, bear in mind that internally they are converted to JSON and sent down a websocket (a process that potentially loses information).
Eello, World!
See full example in: examples/01 - hello_world
Putting this together into a Hello, World! example, we have a short HTML page, web/hello.html:
<!DOCTYPE html>
<html>
<head>
<title>Hello, World!</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">
eel.expose(say_hello_js); // Expose this function to Python
function say_hello_js(x) {
console.log("Hello from " + x);
}
say_hello_js("Javascript World!");
eel.say_hello_py("Javascript World!"); // Call a Python function
</script>
</head>
<body>
Hello, World!
</body>
</html>
and a short Python script hello.py:
import eel
# Set web files folder and optionally specify which file types to check for eel.expose()
# *Default allowed_extensions are: ['.js', '.html', '.txt', '.htm', '.xhtml']
eel.init('web', allowed_extensions=['.js', '.html']
