Create HTML User Interface using Eel in Python
Last Updated :
09 Sep, 2024
Eel is a Python library for making simple 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.
Installation
To install this module, type the below command in the terminal.
pip install eel
Getting Started
First, create a project folder and make another folder called web under it. The web folder consists of all the website files. Create a main.py python file outside the web folder inside project folder.
This should build a folder like below:
Files and FoldersTo use eel in the frontend javascript. Include the following code in the HTML file:
<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 function():
Any python function that is exposed using eel.expose can be called in the javascript like below:
eel.python_function_name()(callback);
Below is an example in which python has a function random_python which returns a random number between 1 and 100, JavaScript calls the and then grabs the returned number and changes a div's innerHTML.
The HTML file here is used to create a window that displays all the required attributes, the java script file will be called by the HTML file to add dynamism to the window created. Python code is used to make this all work.
HTML file:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Eel Example</title>
<style>
h1{
color: green;
text-align: center;
}
.random_number{
margin: 50px;
font-size: 150px;
text-align: center;
}
button{
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>Geeks for Geeks</h1>
<div class="random_number"></div>
<button>Get a Random number using Python</button>
<script type="text/javascript" src="../eel.js"></script>
<script src="./script.js"></script>
</body>
</html>
script.js:
JavaScript
// Onclick of the button
document.querySelector("button").onclick = function () {
// Call python's random_python function
eel.random_python()(function(number){
// Update the div with a random number returned by python
document.querySelector(".random_number").innerHTML = number;
})
}
The main.py contains:
Python
import eel
from random import randint
eel.init("web")
# Exposing the random_python function to javascript
@eel.expose
def random_python():
print("Random function running")
return randint(1,100)
# Start the index.html file
eel.start("index.html")
Output:
Output
Note: Here the java script is calling the python function, when python returns the data it changes the div text.
Similar Reads
Create Interactive Dashboard in Python using Streamlit An interactive and Informative dashboard is very necessary for better understanding our data sets. This will help a researcher for getting a better understanding of our results and after that, a researcher can make some necessary changes for better understanding. Visual Reports is must better than i
6 min read
Create Settings Menu in Python - Pygame Python is a flexible programming language with a large selection of libraries and modules for a variety of applications. Pygame menu is one such toolkit that enables programmers to design graphical user interfaces for games and apps. In this tutorial, we'll look at how to use the pygame menu package
9 min read
Draw Graph Grid Using Turtle in Python Prerequisite: Turtle Programming Basics Turtle is an inbuilt module in Python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen). To move turtle, there are some functions i.e forward(), backward(), etc. Approach: Follow
2 min read
GUI chat application using Tkinter in Python Chatbots are computer program that allows user to interact using input methods. The Application of chatbots is to interact with customers in big enterprises or companies and resolve their queries. Â Chatbots are mainly built for answering standard FAQs. The benefit of this type of system is that cust
7 min read
Create Breakout Game using Python In this article we will build a clone of a famous game, 'Breakout'. We will build this game using one of Python's in-built libraries, Turtle. RequirementsAn IDE like PyCharm.Basics of PythonBasic knowledge of Python-Turtle.Willingness to learn.Project StructureWe want to keep the programming simple.
15+ min read
Snake Game in Python - Using Pygame module Snake game is one of the most popular arcade games of all time. In this game, the main objective of the player is to catch the maximum number of fruits without hitting the wall or itself. Creating a snake game can be taken as a challenge while learning Python or Pygame. It is one of the best beginne
15+ min read
Create Address Book in Python - Using Tkinter Prerequisite: Tkinter In this article, we will discuss how to create an address book in Tkinter using Python. Step by step implementation: Step 1: Creating GUI. In this, we will add all the GUI Components like labels, text area and buttons. Python3 # Import Module from tkinter import * # Create Obje
3 min read
Create virtual environment in Python A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. Using virtual environments is a common practice in Python development as it helps to manage dependencies for different projects, avoiding
3 min read
Creating Your Own Python IDE in Python In this article, we are able to embark on an adventure to create your personal Python Integrated Development Environment (IDE) the usage of Python itself, with the assistance of the PyQt library. What is Python IDE?Python IDEs provide a characteristic-rich environment for coding, debugging, and goin
3 min read
Create First GUI Application using Python-Tkinter We are now stepping into making applications with graphical elements, we will learn how to make cool apps and focus more on its GUI(Graphical User Interface) using Tkinter.What is Tkinter?Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI frameworks, but Tkinter is th
12 min read