<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Utsav Datta on Medium]]></title>
        <description><![CDATA[Stories by Utsav Datta on Medium]]></description>
        <link>https://medium.com/@utsav-datta?source=rss-601b47dd8bdd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QKxb_O4_iFJa2r5CocUArA.png</url>
            <title>Stories by Utsav Datta on Medium</title>
            <link>https://medium.com/@utsav-datta?source=rss-601b47dd8bdd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:04:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@utsav-datta/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Stuxnet, The Weaponizing of Computer Codes]]></title>
            <link>https://utsav-datta.medium.com/stuxnet-the-weaponizing-of-computer-codes-3a3f0ba77715?source=rss-601b47dd8bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a3f0ba77715</guid>
            <category><![CDATA[computer-security]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Utsav Datta]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 12:40:18 GMT</pubDate>
            <atom:updated>2021-02-12T12:40:18.489Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ibPAQ0wn95Qv-7E4" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In 2010 computer scientists uncovered a highly complex malicious computer program whose characteristics were less like a traditional malware and more like a sophisticated cyber-weapon.</p><p>Although classified as a <em>worm, </em>Stuxnet is actually much more than that. Unlike a simple worm which would randomly replicate and infect machines throughout the network, the Stuxnet is a computer program that is carefully engineered to specifically target machines which fulfill certain criteria. The malware is so targeted in its attack that an infected machine can only spread the malware to at-most 3 other machines.</p><p>Once inside the target network, Stuxnet takes control of the <em>programmable logic controllers</em> where it modifies the code to disrupt the normal working of hardware components. Due to this high level of complexity and the malware’s targeted nature, it is alleged that the malware was designed by a specific organization(s) for a very specific purpose. That is, to delay or derail Iran’s nuclear program.</p><h4>Generic Structure of a Cyber-Attack</h4><p>Launching a targeted cyber attack on an organization is a fairly complex process which contains well defined steps of execution.</p><p>Scientists at Lockheed-Martin derived the “Kill-chain” model which outlined the basic phases of a cyber-attack. Although the sequence model was created to defend networks from cyber attacks, but it is also widely accepted by various organizations for breaking down the entire cyber-attack process into phases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xEOh1X5vbz9Z7nCzPi5geQ.png" /><figcaption>The 7 phases of Cyber-Attack</figcaption></figure><p>The chain starts with the attacker collecting as much information as possible about the target system. All the vulnerabilities are noted down for possible exploitation. In the next phase, the attacker designs and develops the exploit that is to be sent to the target machine. This exploit is basically the program that will take advantage of the known vulnerability to perform the malicious action. Finally, the exploit is sent to the target system via email, internet or any other possible medium.</p><p>Once on the target machine, the exploit starts to execute itself and gets installed on the machine. After successful installation, the malware starts to execute the required commands to gain control of the target machine and finally performs the action it was designed to do.</p><h4>Coming Back to Stuxnet</h4><p>Stuxnet comes under the category of Advanced Persistent Threat. This means that Stuxnet gains access to the target network and performs its task in a very stealthy manner, over an extended period of time.</p><p>If we are to compare Stuxnet’s infiltration process with the Cyber-Kill Chain, then the very first step would be to collect as much information as possible about the target (Reconnaissance), based on which the payload and the delivery process would be designed. As it turns out, Stuxnet’s target was an “air gapped” system, which means that it was isolated from unsecured networks like the public internet that we use.</p><p>Due to this nature of the target, physical delivery of the malware was necessary, which was done via thumb drive. Once inside the target network, the malware looks for certain specific criteria. The post-delivery working of the malware is depicted in the following flowchart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vx8f4LCItNJp_wLJo3GzoQ.png" /></figure><p>Stuxnet, acting like a worm, propagates through the network independently and searches out machines that have Siemens Step7 software installed in them. Once this criterion is met, the malware checks if the machine is associated with programmable logic controllers (PLC) which are responsible for automation of mechanical processes. One of which is controlling the operations of “<em>gas centrifuges</em>” for separating nuclear materials.</p><p>The machines that don’t fulfill the above mentioned criteria are not effected by Stuxnet, as the malware lays dormant in them. The machines which do however, are exploited by it.</p><p>Stuxnet modifies the code in PLC so that it provides erroneous commands, causing the centrifuges to behave in an undesirable way. This causes massive physical damage to the centrifuges. While performing this action, Stuxnet makes sure that the PLCs return values that are within the normal range to the checking mechanisms so that it can remain undetected within the system for an extended period of time.</p><p>It is reported that Stuxnet was able to damage almost one-fifth of Iran’s nuclear centrifuges. However, it must be noted that although Iran’s system may have been the primary target for Stuxnet but the malware was NOT limited to Iran. Along with Iran, other countries such as Indonesia, India, Azerbaijan and United States were also majorly affected by the malware. All in all, Stuxnet caused damage to almost 1000 machines.</p><p>Stuxnet’s code and design can be modified to attack modern controllers also which can lead to considerable mayhem. Due to its targeted nature, its ability to cause massive physical damage and the fact that Stuxnet is actually a generic malware that can work on any type of programmable controllers, Ralph Langner, a well respected cyber security expert rightly called Stuxnet a “Cyber weapon of mass destruction”.</p><h4>Some Terminologies</h4><blockquote><strong>Malware :</strong> Malware is a type of software that is specifically designed to cause harm to a computer.</blockquote><blockquote><strong>Worm: </strong>Worm is a sub-category of malware which can self replicate and spread itself independently using a computer network.</blockquote><blockquote><strong>PLC: </strong>PLC stands for Programmable Logic Controllers. These are a type of computers which are used for controlling manufacturing processes.</blockquote><blockquote><strong>Vulnerability:</strong> It is a fault or weakness in cyber-security, that an attacker can take advantage of.</blockquote><blockquote><strong>Exploit:</strong> In cyber-security terms, exploit is basically a program that is designed to take advantage of a known vulnerability.</blockquote><blockquote><strong>Gas Centrifuges:</strong> These are delicate machines which spin at over 100000 rotations per minute. These centrifuges are used to extract heavy concentration of Uranium 235 from the raw ore.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a3f0ba77715" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a Client-Server Relationship using Socket Programming]]></title>
            <link>https://utsav-datta.medium.com/creating-a-client-server-relationship-using-socket-programming-f284ce769648?source=rss-601b47dd8bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/f284ce769648</guid>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[socket-programming]]></category>
            <dc:creator><![CDATA[Utsav Datta]]></dc:creator>
            <pubDate>Mon, 29 Jun 2020 15:10:46 GMT</pubDate>
            <atom:updated>2020-06-29T15:10:46.755Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hIf23ygFMRKAxWB1" /><figcaption>Photo by <a href="https://unsplash.com/@hidd3n?utm_source=medium&amp;utm_medium=referral">Kevin Horvat</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, we are going to discuss the procedure for creating a client-server model in python using sockets. We would be creating a local server in our machine and a client program that will communicate with the server. For the discussed Client-Server model, both the programs (Client and Server) will be running on the same machine.</p><h4>The Server Side</h4><p>While creating the server program (server_side.py), the first step is importing the socket module which would allow us to connect and communicate with different nodes on the network.</p><p>After importing the socket module, we create the socket by passing two parameters which are <strong>socket.AF_INET &amp; socket.SOCK_STREAM. AF_INET </strong>is an address family which specifies that our socket will communicate with Internet Protocol v4 addresses (IPv4). <strong>SOCK_STREAM</strong> indicates that the created socket is of the TCP type. If we want to create an UDP type socket, then we would have to pass <strong>SOCK_DGRAM</strong> instead.</p><pre>import socket<br>skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</pre><p>The next step will be to<strong> bind</strong> the socket. For binding, we need to provide two things, a HOST and a PORT. For HOST, we are using the socket.gethostname command and for PORT, we need to provide a high number as lower valued numbers are already reserved for other well known services. Choosing a high number will ensure that there is no conflict.</p><pre>skt.bind((socket.gethostname(), 9999))</pre><p>After binding the socket, we need to specify how many client connections can be queued before denying anymore. This is done using the listen command.</p><pre>skt.listen(2) </pre><p>Using the above code, we have technically created a server program. The next step is listening to clients that may want to connect with the created server. We achieve this by running an endless while loop.</p><pre>while True:<br>    client, address = skt.accept()<br>    print(&quot;Sending...&quot;)<br>    info = &quot;u are connected&quot;<br>    client.send(bytes(info,&quot;utf-8&quot;))<br>    client.close()</pre><p>Inside an endless while loop, the socket accepts clients that want to connect with us. After accepting the client, we send them a string “u are connected”. However, we cannot directly send a string to the client. We first need to encode the string in <strong>8</strong>-bit Unicode Transformation Format and then send it to the client.</p><h4>The Client Side</h4><p>Coding a client program is simpler compared to coding the server side.</p><pre>import socket<br>client = socket.socket()<br>client.connect((socket.gethostname(),9999))<br>print(client.recv(1024).decode())</pre><p>All we need to do is create the client socket and connect it with the server using the same HOST name and PORT number. In our example, we are receiving what the server is sending using the .recv(1024) function. Here 1024 means that we can read at most 1024 bytes. We also need to decode the received message because we had encoded it in the server side before sending.</p><h4>Implementing in a Project</h4><p>In this section, we will implement socket programming knowledge to create a simple GUI application project which will use a client-server model.</p><blockquote><strong>AIM: </strong>We will create a server program that will contain a dictionary. This dictionary contains items along with the item’s cost. The Client Side will be an User Interface program where the user will provide the item and the quantity. The client will send the user inputs to the server and the server will return the total cost.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mOm1IMIA55oGPAzrwB69vw.png" /><figcaption><strong>Client User Interface: Drop-Down List</strong></figcaption></figure><p>Now, obviously we won’t discuss each and every line of the project but we will however, discuss the important bits. The following figure shows the schematic representation of the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ndxoD7Xk2z46GNP0MFc1-g.png" /><figcaption><strong>System design of the project</strong></figcaption></figure><p>The User interface is designed in HTML &amp; CSS, and for connecting the HTML page with the python program (client.py), we are using the <a href="https://pypi.org/project/Eel/">python Eel</a> library. While using the Eel library, a JavaScript file is also required which acts as a connector between the HTML page and the python program.</p><p>Once the user input is received by the Client python program, we can start with the socket programming part. But, before we look at the client side code, it will be better if we tackle the server side code first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jiivjeqBnCFRsGgTKLaYeQ.png" /><figcaption><strong>Server Side Python Code</strong></figcaption></figure><p>The first thing you might notice is that we have imported “pickle”. We need this module because the client side will send a list to the server which will contain the item’s name and the quantity. To transfer something like a list we first need to “pickle” it before sending and then “unpickle” it after receiving.</p><p>After the computation is done, we convert the result into a string format, encode it in “utf-8” and send it to the client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qC0mrg_KMmWJFAN1TG6UUw.png" /><figcaption><strong>Client Side Python Code</strong></figcaption></figure><p>The user provides the “itemName” and “quantity” in the HTML page, which is then passed on to the python side via JavaScript. In the python function, we store “itemName” and “quantity” as elements of a list and pickle the list for transmission. After creating the socket, we send the list to the server and receive the result that is sent back by the server. This received result is stored as “total” in the client program. It is decoded and the python function returns the total to the JavaScript program which displays it on the HTML page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*ItR6N3plAN2JYQBD9wzvqw.png" /><figcaption><strong>Sequence of events</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WEdoUIa9ySJl___efbOvBg.png" /><figcaption><strong>Client User Interface</strong></figcaption></figure><h4>Summary</h4><p>In this article, we discussed how to use sockets to communicate between multiple programs and exchange information between them. For our examples we have used python programming language but the concept of sockets is similar for most of the languages.</p><p>The steps for creating a program which implements socket can be broken down as follows.</p><ul><li><strong>Server Side</strong></li></ul><ol><li>Create the Socket</li><li>Bind the Socket by providing the Host and Port</li><li>Put the socket into listening mode by using the listen() function</li><li>Create an endless while loop inside which we accept clients, perform operations and send processed data to the clients</li><li>Close the Client after performing the required tasks</li></ol><ul><li><strong>Client Side</strong></li></ul><ol><li>Create the Socket</li><li>Connect to the server side using Host and Port</li><li>Send data to the server side</li><li>Receive and decode the information sent back by the server</li></ol><p>In this particular example, we have used the eel library to connect the client python program to a HTML page. You can refer to the following article for understanding how to use the Python Eel library to create HTML User Interfaces for python programs.</p><p><a href="https://medium.com/wronmbertech/create-html-user-interface-for-python-using-eel-library-bab101cc0f99">Create HTML  User Interface for Python using Eel Library</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f284ce769648" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a GUI Application to Translate Text using Python]]></title>
            <link>https://utsav-datta.medium.com/create-a-gui-application-to-translate-text-using-python-9a4e4ef0e52c?source=rss-601b47dd8bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a4e4ef0e52c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Utsav Datta]]></dc:creator>
            <pubDate>Sat, 13 Jun 2020 08:12:02 GMT</pubDate>
            <atom:updated>2020-06-13T08:12:02.620Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PZLx338xua3VSuWn" /><figcaption>Photo by <a href="https://unsplash.com/@mbaumi?utm_source=medium&amp;utm_medium=referral">Mika Baumeister</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, we are going to discuss the steps for creating a GUI text translator application using python. The Graphic User Interface is designed using HTML along with CSS and the connection between the python program and the HTML page is done using the Python Eel library.</p><p>The Eel library requires us to also create a JavaScript file where we have to define the functions that are triggered using <em>onclick events</em> from the HTML page. In our case, these JavaScript functions basically fetch data from the HTML page and passes it on to the python functions. These JS functions also have a callback function associated with them, which helps us to fetch data from the python side and display them on the HTML page.</p><p>Along with the Eel library, we would also need the ‘Googletrans’ library which uses the Google Translate Ajax API to translate the input text. The working of the program can be schematically represented as following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m57v37z1iVa0udvUvURoQw.png" /><figcaption>Fig1: Schematic representation of the program</figcaption></figure><p>The first step is to install the required libraries, which can be done using pip.</p><ul><li>pip install eel</li><li>pip install googletrans</li></ul><p>We will be using Visual Studio Code for our project, which is files and folders based. Therefore, we create a folder named ‘eel_project’ and inside ‘eel_project’ we create a python file named ‘backend.py’ and another folder named ‘web’. This ‘web’ folder acts as the Front End Directory (Fig1) and contains the following files:</p><ul><li>home.html</li><li>main.js</li><li>style.css, and a picture (pic.jpg) which serves as the background image.</li></ul><h4>Designing the HTML page</h4><p>While designing the HTML page, we have to keep in mind the basic information that are needed by the google API to translate the input text. For efficient translation, we need to provide 3 parameters, which are:</p><ul><li>The input text that needs to be translated</li><li>The source language</li><li>The destination language</li></ul><p>We would also require a text-area in the page where we would display the output text. To take in the input text and to display the output text, we would be using the <em>‘&lt;textarea&gt;’</em> element in HTML. For selecting the source and destination languages, we are using the <em>‘&lt;select&gt;’ </em>element which would create a drop down list, inside which the<em> ‘&lt;option&gt;’</em> tags would define the languages. We would also require a button, clicking which would initiate the translation program. The following snippet shows the required HTML elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h--e2pS4gcbrzNbTEhyl3A.png" /><figcaption><strong>HTML Code Snippet</strong></figcaption></figure><p>We create a <em>‘&lt;form&gt;’</em>, inside which we create two ‘text-areas’ with the ids “data” and “output”. The options tag contains the languages inside the drop-down list created by the select tag. We have created two select tags, one for the source language (id = “source”) and one for the destination language (id = “destination”). The elements of the option tags are:</p><pre>it = Italian <br>ar = Arabic <br>es = Spanish<br>pt = Portuguese<br>fr = French <br>en = English </pre><p>There are plenty of languages available in googletrans, but for this project we are providing six options. Finally, the created button has an onclick event associated with it.</p><pre><strong>&lt;button class=&quot;button1&quot; onclick=&quot;inputdata()&quot;&gt;TRANSLATE&lt;/button&gt;</strong></pre><h4>The JavaScript Part</h4><p>When we click the button, the “<strong>inputdata()</strong>” function, which is defined in the JavaScript file, is triggered. This function fetches the input data from the HTML page, stores them as variable and passes them to the python side.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I4a_9VKHFm_BMbf08NAErA.png" /><figcaption><strong>JavaScript Code</strong></figcaption></figure><p>By using the above JavaScript code, we fetch the required data from the HTML side and store them as variables. The variables that need to be passed to the python side are:</p><pre>var data = The input text <br>var srclang = The source language <br>var destlang = The destination language</pre><p>The <strong>“translate()”</strong> function is defined in the python side and the eel.translate() command calls the python function and passes the above values to it.</p><p>The ‘outputText’ is the translated text which is returned by the python function. The callback function “<strong>set0utput()</strong>” takes this ‘outputText’ and puts it in the HTML <em>&lt;textarea&gt;</em> whose id is “output”.</p><p>Therefore, we can see that the JavaScript file works as a connector between the HTML page and the Python program.</p><h4>The Python Program</h4><p>In the python program, we need to import the required libraries first.</p><pre>import eel<br>from googletrans import Translator</pre><p>The python code that we want to expose to the HTML side needs to be written under an <strong>eel.expose </strong>command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*6x9NfRlSXxDT0cLeU-7Qnw.png" /><figcaption><strong>Python Code</strong></figcaption></figure><blockquote>The <em>eel.init(“front-end folder”)</em> and the <em>eel.start(“html page”) </em>commands are required by the eel library to locate the folder where the user interface files are stored and to start the HTML page.</blockquote><p>We define the required “translate()” function in the python program, with data, srclang and destlang as its arguments which are provided by the user in the HTML page. The python function returns ‘<strong>outputText’ </strong>which is fetched by the <strong>set0utput() </strong>function in the JavaScript file.</p><h4>The Final Product</h4><p>In this article, we have not discussed the CSS component of the project as it does not effect the technical aspect and is only done for beautification purpose. However, the entire code is available on my GitHub profile <a href="https://github.com/UtsavDatta/eel_translator">here</a>.</p><p>On completion, the final User Interface looks as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*9zouwPrZ4np5qwGBDpl0qw.jpeg" /><figcaption><strong>User Interface</strong></figcaption></figure><blockquote>By default the eel library uses the chrome browser to run the program. However, this can be changed by using the <em>‘mode’</em> option, through which we can specify what browser to use.</blockquote><p>Executing the project that we described in this article is a great way to familiarize yourself with the python eel library which is used to create modern looking User Interface in HTML and CSS for python programs.</p><h4>Summary</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ILS9khS8GnGHMha77X4Zg.png" /><figcaption><strong>Flow of the program</strong></figcaption></figure><p>In this article we discussed the steps for creating a text translation app in python using the googletrans library. We have used HTML along with CSS for designing the User Interface of the project and have used the python eel library for connecting the python program with the HTML page via JavaScript.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a4e4ef0e52c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create HTML  User Interface for Python using Eel Library]]></title>
            <link>https://utsav-datta.medium.com/create-html-user-interface-for-python-using-eel-library-bab101cc0f99?source=rss-601b47dd8bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/bab101cc0f99</guid>
            <category><![CDATA[python-libraries]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[user-interface]]></category>
            <dc:creator><![CDATA[Utsav Datta]]></dc:creator>
            <pubDate>Fri, 05 Jun 2020 16:58:51 GMT</pubDate>
            <atom:updated>2020-06-05T16:58:51.952Z</atom:updated>
            <content:encoded><![CDATA[<h3>Create HTML User Interface for Python using Eel Library</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*9zouwPrZ4np5qwGBDpl0qw.jpeg" /></figure><p>In this little article, we are going to discuss the use of python Eel library to create User Interface for python programs using HTML and CSS.</p><p>The article is divided into two parts. In the first part, we discuss the basic theory of the Eel library and in the second part, we show the implementation of the library by creating a simple application which takes 2 numbers as input from the user and returns their sum.</p><h3>PYTHON Eel LIBRARY</h3><p>To put it simply, the eel library is used to create HTML User Interfaces for offline python applications. While there are tools such as Tkinter, where the GUI can be coded in python itself, but the limitations are quite high and it doesn’t give as much freedom as we get from using HTML and JavaScript.</p><p>This is where Eel comes in. It allows us to create offline applications, where the front end is coded in HTML and CSS while the actual program logic runs behind in python. This can be visualized by the following figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*iFfbU3Gs4osBVbxsyKp0Gg.png" /><figcaption>Figure 1</figcaption></figure><p>The HTML, CSS and JavaScript files are kept in the same directory and is initialized using the python program. The JavaScript file basically helps in connecting the HTML page to Python by using functions which are triggered by onclick events from the HTML page.</p><h3>IMPLEMENTING THE LIBRARY</h3><p>In this section, we are going to make a simple application using eel for illustrative purpose. In this application, we are going to take 2 numbers from the HTML page, add them in python and display the result to the user.</p><p>The First step is to install the Eel library which can be done through pip.</p><pre><strong>pip install eel</strong></pre><p>We are using Visual Studio Code for creating the project. We open a new folder named “testweb” and create another folder named “web” under it. This “web” folder contains the following files.</p><ul><li>home.html</li><li>main.js</li><li>add.py</li></ul><p>In the HTML page, we create 3 text-areas and a button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vai0JZ1Lj9A8LwcioZ6i_Q.png" /><figcaption><strong>HTML Code</strong></figcaption></figure><p>The ‘num1’ and ‘num2’ are for getting the input from the user and ‘ans’ text-area is for displaying the result. The button with the id = ‘add’ has an onclick function attached to it which is defined in the JavaScript file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvHLNTu2uK0p0b9YBShbsw.png" /><figcaption><strong>JavaScript Code</strong></figcaption></figure><p>In the JavaScript file, we create the function that is triggered using the onclick event. In the function we fetch the value of ‘num1’ and ‘num2’ and pass it to the “add” function, which is defined in the python file. The “callBack” function takes ‘result’ as the argument which is returned by the python function “add”. This ‘result’ is put in the text-area whose id is ‘ans’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*cspRc0125LhtxhxmDcc1mQ.png" /><figcaption><strong>Python Code</strong></figcaption></figure><p>In the python code, we initialize the application using the <strong><em>eel.init(“folder name”)</em></strong><em> </em>command. The folder name in our case is <strong>“web”</strong>.</p><p>Under the<em> eel.expose</em> command, we define the “add” function which adds ‘data1’ and ‘data2’ and returns the ‘result’. This ‘result’ is fetched by the “callBack” function in the JavaScript file and is placed in the HTML page.</p><p>We start the application using the <strong><em>eel.start(“html file”)</em></strong> command. In our case which is <strong>“home.html”</strong>.</p><h4>The following figure shows the created User Interface:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*mVYX7ClrGBgErQY473uf-A.png" /><figcaption><strong>User Interface</strong></figcaption></figure><p>By default, the Eel library uses chrome to run the application but we can also specify the browser by using the ‘mode’ option. Along with ‘mode’ there are several other app options available such as ‘size’, ‘position’, ‘geometry’ etc. which are passed in the eel.start() command.</p><h3>FINAL THOUGHTS</h3><p>In this brief article, we discussed about the python Eel library, which allows us to create offline HTML/JavaScript applications. By using this library, we get the capabilities of python, along with the freedom of designing User Interface with HTML and CSS. Although popularity wise, eel is below its main competitor, Electron, but for creating simple GUI applications, many choose eel as their first choice.</p><p>In a follow up article, we are going to implement the Eel library to create a text translating application using HTML, CSS, JavaScript and Python.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bab101cc0f99" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>