<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kodark</title>
    <description>The latest articles on DEV Community by kodark (@kodark).</description>
    <link>https://dev.to/kodark</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F518400%2F18106c13-759a-4e42-9f82-4d393c96b483.png</url>
      <title>DEV Community: kodark</title>
      <link>https://dev.to/kodark</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kodark"/>
    <language>en</language>
    <item>
      <title>Executable for our demo application</title>
      <dc:creator>kodark</dc:creator>
      <pubDate>Sat, 20 Feb 2021 06:22:48 +0000</pubDate>
      <link>https://dev.to/kodark/executable-for-our-demo-application-48ol</link>
      <guid>https://dev.to/kodark/executable-for-our-demo-application-48ol</guid>
      <description>&lt;p&gt;In our previous posts we have explored &lt;a href="https://github.com/ChrisKnott/Eel"&gt;Eel&lt;/a&gt; library, along with its options and we have created a demo application.&lt;/p&gt;

&lt;p&gt;This post is mainly dedicated to pyinstaller, which is a python library which allows to easily distribute python code. Pyinstaller will help us to distribute our eel application.&lt;/p&gt;

&lt;p&gt;First step is to install pyinstaller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pyinstaller
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pyinstaller also supports encryption of Python bytecode modules, in order to use this feature, it must be installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pyinstaller[encryption]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we have installed the required libraries, we are going to explore how can we distribute our application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Create exe file (windows)
&lt;/h1&gt;

&lt;p&gt;Remember to &lt;code&gt;cd&lt;/code&gt; to our project folder, our python script is named app.py and "web" folder contains out html, css and js. We can create an exe file (under windows, .so under mac or linux) with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m eel app.py web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pyinstaller will analyze our python script, will create a spec file, will create a folder named build for holding log and working files and finally will create a folder named dist for writing the executable. Dist folder is the one we should send to our users.&lt;/p&gt;

&lt;p&gt;The output from the previous command will be a folder (dist) which contains all python dependencies and our exe file, which is the one we should run. However, it would be better to distribute a single file instead of a folder with several number of folders, subfolders and files. &lt;/p&gt;

&lt;p&gt;A single executable file can be built with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m eel app.py web --onefile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Hiding console window
&lt;/h1&gt;

&lt;p&gt;With the previous command, a single file has been created, this is easier to distribute and for the user; however, if we run our exe file we can notice that the chrome window appears but a command window also does.&lt;/p&gt;

&lt;p&gt;Hiding the command window can be achieved by adding a flag to our current command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m eel app.py web --onefile --noconsole
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have a single executable file and once it is executed, a single window will show up. Finally, what we can do is add encryption of our bundled files, this can be achieved with the key flag and a key of lenght equal to 16.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m eel app.py web --onefile --noconsole --key=abcdefghijklmnop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are capable of distributing our python applications with a user friendly GUI in a single executable file. &lt;/p&gt;

&lt;p&gt;There is still one issue, executable files will work only on the same platform where it was built, e.g., an executable file generated on windows 10 - 64 bit won't work on windows 10 -32 bit, or in windows Server. Obviously, it also won't work on MAC OS or linux based OS. &lt;/p&gt;

&lt;p&gt;So you must build your executable on all the platforms where you want to distribute your application. For achieving this, you can explore task automation tools which allows you to perform continuous integration and continuous delivery.&lt;/p&gt;

&lt;p&gt;One CI/CD popular tool, is Jenkins. We might have a Jenkins series in the near future.&lt;/p&gt;

</description>
      <category>python</category>
      <category>eel</category>
      <category>pyinstaller</category>
      <category>exe</category>
    </item>
    <item>
      <title>Eel options</title>
      <dc:creator>kodark</dc:creator>
      <pubDate>Wed, 20 Jan 2021 13:44:04 +0000</pubDate>
      <link>https://dev.to/kodark/eel-options-4hhj</link>
      <guid>https://dev.to/kodark/eel-options-4hhj</guid>
      <description>&lt;p&gt;In a previous &lt;a href="https://dev.to/kodark/creating-a-modern-gui-for-your-python-application-2mlp"&gt;post&lt;/a&gt;, we explore &lt;a href="https://github.com/samuelhwilliams/Eel"&gt;Eel&lt;/a&gt;, which is a cool python library that allow us to create web based GUI's for our python scripts.&lt;/p&gt;

&lt;p&gt;A simple application was created, using the minimal arguments for eel.start; in this post we're going to explore what other options we have in order to better fit our requirements/needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  mode
&lt;/h3&gt;

&lt;p&gt;mode will allow us to select which browser should be used to render our html files; default is set to chrome browser. Another options that can be used are: 'edge' for edge browser, 'electron' and 'chrome-app' for firefox.&lt;/p&gt;

&lt;h3&gt;
  
  
  host
&lt;/h3&gt;

&lt;p&gt;host is used to specify the hostname for the underlying libraries that eel makes use; default is localhost.&lt;/p&gt;

&lt;h3&gt;
  
  
  port
&lt;/h3&gt;

&lt;p&gt;port is one of the most useful options, sometimes the application won't start because default port is being used by other application, this should be fixed by specifying another port. Default value is 8000.&lt;/p&gt;

&lt;h3&gt;
  
  
  block
&lt;/h3&gt;

&lt;p&gt;By default, when python interpreter reach eel.start line, application will be blocked, thus, remaining code won't be executed. If you want to run eel and not get stuck at that line, you can set block value to False.&lt;/p&gt;

&lt;h3&gt;
  
  
  size
&lt;/h3&gt;

&lt;p&gt;size allow us to set the window size, it must be specified as a tuple containing width and height.&lt;/p&gt;

&lt;h3&gt;
  
  
  position
&lt;/h3&gt;

&lt;p&gt;position allow us to specify the origin coordinates (top-left corner) of the window, it is also a tuple, but contains x-axis (horizontal) and y-axis (vertical). Remember, 0,0 is top-left corner.&lt;/p&gt;

&lt;h3&gt;
  
  
  geometry
&lt;/h3&gt;

&lt;p&gt;Instead of using size and position option, you can pass a dictionary to geometry option. This dictionary requires keys size and position, and values for both are tuples. E.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;geometry={'size': (700, 400), 'position': (50, 50)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  disable-cache
&lt;/h2&gt;

&lt;p&gt;Default is false, specifying True value will prevent the browser to cached our website.&lt;/p&gt;

&lt;h3&gt;
  
  
  close_callback
&lt;/h3&gt;

&lt;p&gt;Allow us to modify what python should do when closing the window. By default, python will exit upon closing the window, which could lead to data loss if we closed accidentally, so we can modify default behavior to properly closed running tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  cmdline_args
&lt;/h3&gt;

&lt;p&gt;We can add extra flags in order to start the browser, for example, running on incognito mode, disabling backgrounds or prompting a dialog before the browser starts. You can found useful flags &lt;a href="https://www.thewindowsclub.com/chrome-command-line-switches"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;p&gt;Now that we have explore other options for eel, we'll update our python script from our previous &lt;a href="https://dev.to/kodark/creating-a-modern-gui-for-your-python-application-2mlp"&gt;post&lt;/a&gt;. HTML, CSS and JS files remains the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import eel
import random
from datetime import datetime

def close_callback(route, websockets):
    if not websockets:
        print('Bye!')
        exit()

eel.init('web')

@eel.expose
def get_random_name():
    eel.prompt_alerts('Random name')

@eel.expose
def get_random_number():
    eel.prompt_alerts(random.randint(1, 100))

@eel.expose
def get_date():
    eel.prompt_alerts(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))

@eel.expose
def get_ip():
    eel.prompt_alerts('127.0.0.1')

eel.start('index.html', mode='chrome', 
                        host='localhost', 
                        port=27000, 
                        block=True, 
                        size=(700, 480), 
                        position=(0,0), 
                        disable_cache=True, 
                        close_callback=close_callback, 
                        cmdline_args=['--browser-startup-dialog', 
                                '--incognito', '--no-experiments'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is different now?
&lt;/h3&gt;

&lt;p&gt;We just added a new function before eel.init, close_callback, this function allow us to modify default behavior upon closing the window; instead of only exit, it will first print "Bye!" in our python cosole. This is just a "dummy" test, but you can later update a file, update a database or simply perform a failsafe exit.&lt;/p&gt;

&lt;p&gt;The other changes are simply the bunch of arguments we're passing to eel.start; we have added mode, block and localhost with its default values (there is no need to pass this arguments since we're using chome, True and localhost); with size we're determining the size of the browsers window, and with position, we specify that it must be start at the top-left corner. We're also changing default port, which is 8000 to 27000, the cache is also disable and for the close_callback argument, we specify our custom function.&lt;/p&gt;

&lt;p&gt;Finally, for the cmdline_args we passed three arguments, first one will prompt an alert window which allows the user to know that the browser is starting and its PID. Second element, simply specifies to use an incognito window and third element prevent chrome from using experimental flags (like dark mode).&lt;/p&gt;

&lt;p&gt;We have explored additional options for eel and updated our previous demo application with this options. In our next series we'll explore how to create an executable.&lt;/p&gt;

</description>
      <category>python</category>
      <category>eel</category>
    </item>
    <item>
      <title>Creating a modern GUI for your python application </title>
      <dc:creator>kodark</dc:creator>
      <pubDate>Tue, 22 Dec 2020 03:21:04 +0000</pubDate>
      <link>https://dev.to/kodark/creating-a-modern-gui-for-your-python-application-2mlp</link>
      <guid>https://dev.to/kodark/creating-a-modern-gui-for-your-python-application-2mlp</guid>
      <description>&lt;p&gt;In this post, we are going to use a library which will allow us to create outstanding graphical user interfaces (GUI's) for our day to day python scripts.&lt;/p&gt;

&lt;p&gt;We have a bunch of options to create GUI's on python, tkinter, wxPython, PySimpleGui, among others. Tkinter is really robust and allow us to have cross-platform applications, however, Tkinter based GUI's aren't as beautiful as what we can typically find with web based applications, and other cool applications based on electron. &lt;/p&gt;

&lt;p&gt;Eel is a library which allow us to take advantage of web technologies, such as HTML, CSS, JS and of course, all bunch of web frameworks out there, as bootstrap, jquery, cool plotting libraries as plotly, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Eel
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://github.com/samuelhwilliams/Eel" rel="noopener noreferrer"&gt;Eel library&lt;/a&gt; allow us to easily make use of HTML, CSS and JS for building our User Interface, without losing all our powerful Python capabilities. Eel relies on a bunch of python libraries, it simply creates a local webserver and opens a browser (chrome by default), the browser renders HTML, CSS and JS, while python controls most of the logic. &lt;/p&gt;

&lt;p&gt;Cool thing with Eel, is that allow us to run python functions from javascript and viceversa, thus, we can exchange information and have the best of both worlds (python and JS).&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing eel
&lt;/h1&gt;

&lt;p&gt;In order to install eel library, you can do it from Pypi as:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pip install eel


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Highly recommended to create a virtual environment first.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to use it?
&lt;/h1&gt;

&lt;p&gt;We must create our folder structure first, our root folder will be "application". Inside, we will create our python script, which I decided to call app.py. &lt;/p&gt;

&lt;p&gt;In order to better structure our code, we created a subfolder named "web", which contains index.html file, and two subfolders for our css and js files.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

application
│   app.py
│
└───web
    │   index.html
    │
    ├───css
    │       main.css
    │
    └───js
            main.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For this post, we will build a top menu resembling a file menu for desktop applications using pre-built code from &lt;a href="https://www.w3schools.com/css/css_navbar_horizontal.asp" rel="noopener noreferrer"&gt;W3schools&lt;/a&gt;. First, we must edit our html file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Eel example&amp;lt;/title&amp;gt;

    &amp;lt;link rel="stylesheet" href="css/main.css"&amp;gt;
    &amp;lt;script type="text/javascript" src="/eel.js"&amp;gt;&amp;lt;/script&amp;gt; 
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="topnav"&amp;gt;
        &amp;lt;a class="active" id="button-name"&amp;gt;Display name&amp;lt;/a&amp;gt;
        &amp;lt;a id="button-number"&amp;gt;Display a random number&amp;lt;/a&amp;gt;
        &amp;lt;a id="button-date"&amp;gt;Display time and date&amp;lt;/a&amp;gt;
        &amp;lt;a id="button-ip"&amp;gt;Display my ip&amp;lt;/a&amp;gt;
      &amp;lt;/div&amp;gt; 
&amp;lt;/body&amp;gt;
&amp;lt;script src="js/main.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, you would be able to create your whole GUI using HTML, we just must add &lt;code&gt;&amp;lt;script type="text/javascript" src="/eel.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;, this script will allow us to call our exposed python functions.&lt;/p&gt;

&lt;p&gt;For CSS file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

/* Add a black background color to the top navigation */
.topnav {
    background-color: #333;
    overflow: hidden;
  }

  /* Style the links inside the navigation bar */
  .topnav a {
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
  }

  /* Change the color of links on hover */
  .topnav a:hover {
    background-color: #ddd;
    color: black;
  }

  /* Add a color to the active/current link */
  /* .topnav a.active {
    background-color: #4CAF50;
    color: white;
  } */


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And finally, for JS file, we're just adding event listeners to out four buttons in navbar. As you can see, all of them are calling some functions but started with eel, e.g. ell.get_random_name(), this is a python function, thus, when we click the button, it will call a python function. Also, we have a JS function called prompt_alerts and it has a decorator eel.expose, this allow us to run this JS function from python:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

document.getElementById("button-name").addEventListener("click", ()=&amp;gt;{eel.get_random_name()}, false);
document.getElementById("button-number").addEventListener("click", ()=&amp;gt;{eel.get_random_number()}, false);
document.getElementById("button-date").addEventListener("click", ()=&amp;gt;{eel.get_date()}, false);
document.getElementById("button-ip").addEventListener("click", ()=&amp;gt;{eel.get_ip()}, false);

eel.expose(prompt_alerts);
function prompt_alerts(description) {
  alert(description);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We have completed our graphical user interface, now, we must create our python file. We must import eel library, and all other libraries you need for your project, in this case, random and datetime.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import eel
import random
from datetime import datetime

eel.init('web')

@eel.expose
def get_random_name():
    eel.prompt_alerts('Random name')

@eel.expose
def get_random_number():
    eel.prompt_alerts(random.randint(1, 100))

@eel.expose
def get_date():
    eel.prompt_alerts(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))

@eel.expose
def get_ip():
    eel.prompt_alerts('127.0.0.1')

eel.start('index.html')


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, we must add our eel.init line, this initialize eel library, to an specific folder. Then, we create our four functions, which we want to call from JS, that's why we added decorator &lt;code&gt;@eel.expose&lt;/code&gt;. As you can see, each one is calling our JS function &lt;code&gt;prompt_alerts&lt;/code&gt; and passing a string argument.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;eel.start&lt;/code&gt; will run the application, there are a lot of arguments that can be passed, in this example, we're using minimal arguments, we're just setting the html file to be rendered (relative to web folder).&lt;/p&gt;

&lt;p&gt;Now that we have completed our code, we can start our application:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
 &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A new window should appeared, as shown in next figure:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm1rpp2yan906gasoldvr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm1rpp2yan906gasoldvr.png" alt="Python GUI using eel library"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you click on any of your four buttons, an alert should be shown, e.g.:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbyh3hsqfhyfulmfjlf3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbyh3hsqfhyfulmfjlf3l.png" alt="Alert example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you're able to create outstanding GUI's for you python applications, in our next series, we will create a complex example and explore other arguments that can be passed to eel.start.&lt;/p&gt;

</description>
      <category>python</category>
      <category>eel</category>
      <category>html</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
