Monday, April 23, 2018

The Making of a Python App


Zachary Farley is one of our new contributor  , He will mainly contribute articles in python language.
Hope he will be able to help you guys in python concepts. Let see this first article about Python App.
Author: Zachary Farley



Introduction

Greetings readers. I’m a beginner self taught programmer. A year ago I decided to take up python as my first programming language because it’s a beginner friendly language. To make learning the language easier I decided to design an App to make managing my hobby easier. I enjoy reading online manga during my free time. I find so many great new content released that I have trouble keeping track of it all. Normally I would add new content to my favorites bar but eventually the listing would get so long it’ll clutter up my other favorites bookmarked.Plus more times than often I’ve lost track of new chapters released by the authors. To combat this issue I decided to develop a desktop application I could use to keep track of new series I find.

The Kivy library

To create the application I decided to use the kivy library since the User Interface (UI) is much nicer compared to other python graphical user interface (GUI) modules such as Tkinter and PyQT. Using the Kivy builder function it allowed creating the GUI layout relatively easy


The builder module allows the layout and the code to be separate by storing it in a. kv file, keeping the code clean and less clunky. The function used for the application was Builder.load_string(). This function loads the layout within the python file instead of using the. kv file. Along with the builder.load_string() function numerous other functions play a key role in bringing the app to life.

Along with the functions to run the app there are layouts that make up the graphical user interface. The form uses BoxLayout to place a label and text_input widget together to collect user input of the name, URL and image or image address of the user.

BEFORE Submission

Once the information is collected and the form is submitted the information then goes to a SQLite database (DB). Once the information gets to the DB a method within the main class runs the SQL statement “select * from manga_list;” which pulls the newly inserted information.

After submission

Once the data is retrieved from the table a row is populated containing 3 widgets: Label, Image/Asyncimage and Button. The label widget takes user input to populate the text property of the label class. The Image widget uses either an image address (ex: http://media.comicbook.com/2018/02/boruto-1086841-1280x0.jpeg) or an image in the local image directory (C:\appfolder\images\boruto.png) which is populated when the application is ran on a new user machine. 

When selecting the “Updates?” button at the bottom of the window the screen changes with the help of the screen manager module within the kivy library.

Second screen to manage updates

BeautifulSoup4

The updated screen uses the web scraping feature of the beautifulsoup python module. When pressing the REALOAD (reload) DATA button the application uses the website “mangatown.com” to look up the most popular updates that have come out, and then populates the information in the window.

Recent updates from online source

Similar to the previous screen the web crawler looks for specific classes within certain HTML tags. In the below box the chapter_list variable looks for <div> with the class “manga_text_content” and turns the info into an iterable array. Then the variable hot_updates looks for the class ‘hot’ within that array.


Variables used to obtain updated info
chapter_list = soup.find('div', attrs={'class': 'manga_text_content'})

hot_updates = chapter_list.find_all('span', attrs={'class': 'hot'})
A basic understanding of HTML coding is needed to obtain the the information desired. Of course each website has its own set of terms and conditions that either allows or disallows web crawlers to search through their website.

SQLite3

Using pythons built in SQLite3 module I stored all the information submitted from the user.
SQLite allows for easy connection to be made without needing to set up a database server such as mysql. It creates a lightweight database file with the extension “.db”. To connect to the DB, the python file and the “*.db” file need to only be in the same directory.

The below code uses the variable BASE_DIR to find the absolute path of the users system no matter the operating system. The db_path combines the absolute path to the newly created SQLite DB named “manga.db”. Finally the conn variable forms the connection and allows SQL statements to be ran using statements like “cur.execute(“SELECT * FROM manga_list;”)”

Python connection variables

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "manga.db")
conn = sqlite3.connect(db_path)
Just as a final check to confirm the data is stored where it should be, SQLite Studio is the database management system used to view the contents without needing to command line.

SQLite3 table contents:

Pyinstaller

For easy distribution to windows systems Pyinstaller was used. Pyinstaller collects the libraries and packages used by the script and allows an executable to be made that will allow launching to any windows pc system. The following command produced a single application that executes beautifully when clicked. For more information on the parameters used when executing the command, please refer to the Pyinstaller docs.

Pyinstaller command used to package application

pyinstaller --clean --log-level DEBUG --onedir --onefile --distpath pckg_app\ --noconsole mangaapp_vX.py


Conclusion

The final product created is a simple tool I use daily to help manage new releases I find and are recommended to read. I hope this tool can be used by others who find it helpful as well. I’ve gone through multiple TEST and PROD versions, the most recent being version 6. Feel free to view the TEST and PROD code of the previous versions as well: (https://github.com/zeke13210/python_project/tree/master/kivy-app)

Thanks for reading
Noeik

0 comments:

Post a Comment