Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code to highlight major advantages of TagUI for Python #25

Closed
ck81 opened this issue Jul 6, 2019 · 4 comments
Closed

Sample code to highlight major advantages of TagUI for Python #25

ck81 opened this issue Jul 6, 2019 · 4 comments
Labels

Comments

@ck81
Copy link

ck81 commented Jul 6, 2019

Thanks to @kensoh in highlighting the major differences between TagUI and TagUI-Python in Issue #8.

In the reply, Ken mentioned that "TagUI for Python only has a subset of TagUI's features." I have been wondering why then do people want to switch to TagUI-Python when TagUI has more features, and TagUI-Python actually runs on top of TagUI (which means that there's an additional layer of TagUI between TagUI-Python and the website).

Decided to convert some of my TagUI scripts into TagUI-Python so that I can have some real comparisons between the two.

After one week of testing TagUI-Python, I must say that I was truly surprised and amazed by TagUI-Python!

First, TagUI-Python can do almost everything that TagUI can do. Conversion is very simple and straightforward - just a change of the syntax. The flow and xpath remains exactly the same, as can be seen in the following sample code.

Second, TagUI-Python is written as a native python package and run as a pure Python script. This means that you can leverage any standard Python packages out there, such as pandas used in the sample code below to save the data as a csv file.

Third, using TagUI-Python, you can now leverage on Python's native data structure (such as lists and classes), and also modules/packages to organize libraries of functions.

So below is a sample code that reads the title and url of a list of books from goodreads.com (total 18 books in that page).

Sample Code 1 - in TagUI

//goodreads.gui
//190706

https://www.goodreads.com/list/show/17347.Books_that_ll_help_you_deliver_stunning_presentations
 
read //h1[contains(@class, "h1")] to list_title
echo "list_title = " + list_title
 
num_books = count('//td/a//span[@itemprop="name"]')
echo "num_books = " + num_books

csv_header = '"Sno", "Title", "URL"\r\n'
dump csv_header to book_list.csv
 
for (n=1; n<=num_books; n++)
{
	read (//td/a//span[@itemprop="name"])['+n+'] to book
	echo "book = " + book
 
	read (//td/a/@href)['+n+'] to url2
	echo "url2 = " + url2
 
	record = '"' + n + '","' + book + '","' + url2 + '"' 
    write record to book_list.csv
}

Sample Code 2 - in TagUI-Python

In comparison, the following is the same robot re-written using TagUI-Python

from pandas import DataFrame
import tagui as t
t.init()

t.url('https://www.goodreads.com/list/show/17347.Books_that_ll_help_you_deliver_stunning_presentations')

list_title = t.read('//h1[contains(@class, "h1")]')
print("list_title = " + list_title)

num_books = t.count('//td/a//span[@itemprop="name"]')
print("num_books = ", num_books)

books = [''] * num_books
url = [''] * num_books
sno = [''] * num_books
for n in range(1, num_books+1):
	sno[n-1] = n

	books[n-1] = t.read(f'(//td/a//span[@itemprop="name"])[{n}]/..')
	print(n, "book = "+books[n-1])

	url[n-1] = t.read(f'(//td/a/@href)[{n}]')
	print("   url = "+url[n-1])

Books_Info = {'Sno':sno, 'Title': books, 'URL': url}
df = DataFrame(Books_Info, columns=['Sno','Title', 'URL'])
export_csv = df.to_csv ('book_list1.csv', index = None)

Note the use of pandas to save data to .csv file. With TagUI-Python, you can now leverage on all the standard python packages out there, including those machine learning libraries including Numpy, Scikit-learn, TensorFlow, Keras, etc.! This will be one of the major reason for moving to TagUI-Python if you're working on machine learning or intelligent RPA project.

@ck81
Copy link
Author

ck81 commented Jul 6, 2019

As TagUI-Python is running within a native Python script, you can re-organize the code with functions as follows:

Sample Code 3 - using functions

from pandas import DataFrame
import tagui as t
t.init()

def read_list_title():
	list_title = t.read('//h1[contains(@class, "h1")]')
	print("list_title = " + list_title)

def read_element(selector):
	t.hover(selector)
	val = t.read(selector)
	return val

def read_book_list():
	num_books = t.count('//td/a//span[@itemprop="name"]')
	print("num_books = ", num_books)
	sno = [''] * num_books
	books = [''] * num_books
	url = [''] * num_books

	print('rpa_lib::read_book_list', num_books)
	print('rpa_lib::books', books)
	print('rpa_lib::url', url)
	for n in range(1, num_books+1):
		sno[n-1] = n

		books[n-1] = read_element(f'(//td/a//span[@itemprop="name"])[{n}]/..')
		print(n, "book = "+books[n-1])

		url[n-1] = read_element('(//td/a/@href)[{}]'.format(n))
		print("   url = "+url[n-1])

	print("books", books)
	print("url", url)
	Books_Info = {'Sno':sno, 
		'Title': books, 
		'URL': url
	}
	save_to_csv(Books_Info, 'book_list2.csv')

def save_to_csv(data, csv_file):
	df = DataFrame(data, columns=['Sno','Title', 'URL'])
	export_csv = df.to_csv (csv_file, index = None)

# main program starts here
t.url('https://www.goodreads.com/list/show/17347.Books_that_ll_help_you_deliver_stunning_presentations')
read_list_title()
read_book_list()

@ck81
Copy link
Author

ck81 commented Jul 6, 2019

You can also choose to put all those functions into a separate file and load them as a Python module.

This, I think, will be another main reason to move over to TagUI-Python. In TagUI, the only way you can re-use the code is to put them into a separate file and load them using the tagui command. However, each file can contain only one 'function'. There's no way you can put multiple functions into one file. This makes re-use and maintenance of TagUI scripts quite challenging.

With TagUI-Python, you can now maintain libraries of functions across multiple modules!

Sample Code 4a - main file

from pandas import DataFrame
import rpa_lib as rpa

import tagui as t
t.init()

t.url('https://www.goodreads.com/list/show/17347.Books_that_ll_help_you_deliver_stunning_presentations')
rpa.read_list_title()
rpa.read_book_list()

Sample Code 4a - save this as rpa_lib.py

import tagui as t
from pandas import DataFrame

def read_list_title():
	list_title = t.read('//h1[contains(@class, "h1")]')
	print("list_title = " + list_title)

def read_element(selector):
	t.hover(selector)
	val = t.read(selector)
	return val

def read_book_list():
	num_books = t.count('//td/a//span[@itemprop="name"]')
	print("num_books = ", num_books)
	sno = [''] * num_books
	books = [''] * num_books
	url = [''] * num_books

	print('rpa_lib::read_book_list', num_books)
	print('rpa_lib::books', books)
	print('rpa_lib::url', url)
	for n in range(1, num_books+1):
		sno[n-1] = n

		books[n-1] = read_element(f'(//td/a//span[@itemprop="name"])[{n}]/..')
		print(n, "book = "+books[n-1])

		url[n-1] = read_element('(//td/a/@href)[{}]'.format(n))
		print("   url = "+url[n-1])

	print("books", books)
	print("url", url)
	Books_Info = {'Sno':sno, 
		'Title': books, 
		'URL': url
	}
	save_to_csv(Books_Info, 'book_list3.csv')

def save_to_csv(data, csv_file):
	df = DataFrame(data, columns=['Sno','Title', 'URL'])
	export_csv = df.to_csv (csv_file, index = None)
	print (df)

@ck81
Copy link
Author

ck81 commented Jul 6, 2019

Another feature that might interest some of you is that with TagUI-Python you can now write the script using full OOP (object-oriented programming), as illustrated in the sample code below.

The OOP together with inheritance is an effective way in reducing code duplication and allows one to better manage code re-use.

Sample Code 5a - main file

import tagui as t

import class_RPA
from class_RPA import *

t.init()

t.url('https://www.goodreads.com/list/show/17347.Books_that_ll_help_you_deliver_stunning_presentations')

goodreads = GoodReads()
goodreads.read_list_title()
goodreads.read_book_list()

Sample Code 5b - save this as class_RPA.py

import tagui as t
from pandas import DataFrame

class RPA:

	def read_element(self, selector):
		t.hover(selector)
		val = t.read(selector)
		return val

	def save_to_csv(self, data, csv_file):
		df = DataFrame(data, columns=['Sno','Title', 'URL'])
		export_csv = df.to_csv (csv_file, index = None)
		print (df)

class GoodReads(RPA):

	def read_list_title(self):
		list_title = t.read('//h1[contains(@class, "h1")]')
		print("list_title = " + list_title)

	def read_book_list(self):
		num_books = t.count('//td/a//span[@itemprop="name"]')
		print("num_books = ", num_books)
		sno = [''] * num_books
		books = [''] * num_books
		url = [''] * num_books

		print('rpa_lib::read_book_list', num_books)
		print('rpa_lib::books', books)
		print('rpa_lib::url', url)
		for n in range(1, num_books+1):
			sno[n-1] = n

			#books[n-1] = t.read(f'(//td/a//span[@itemprop="name"])[{n}]/..')
			books[n-1] = self.read_element(f'(//td/a//span[@itemprop="name"])[{n}]/..')
			print(n, "book = "+books[n-1])

			#url[n-1] = t.read(f'(//td/a/@href)[{n}]')
			url[n-1] = self.read_element('(//td/a/@href)[{}]'.format(n))
			print("   url = "+url[n-1])

		print("books", books)
		print("url", url)
		Books_Info = {'Sno':sno, 
			'Title': books, 
			'URL': url
		}
		self.save_to_csv(Books_Info, 'book_list4.csv')

@kensoh kensoh changed the title A sample code to highlight one major advantage of TagUI-Python Sample code to highlight major advantages of TagUI-Python Jul 6, 2019
@kensoh kensoh changed the title Sample code to highlight major advantages of TagUI-Python Sample code to highlight major advantages of TagUI for Python Jul 6, 2019
@kensoh kensoh added the feature label Jul 6, 2019
@kensoh
Copy link
Member

kensoh commented Jul 6, 2019

Wow CK, thank you so much for your generous sharing! Yes you've covered the major strategic advantages of TagUI for Python vs TagUI. By bringing the capabilities of TagUI directly into the Python ecosystem in the programming language that Python users are already familiar with, it unlocks value creation very quickly. This is because there is little learning curve to get things done.

There are also major advantages of TagUI for Python over a number of Python packages for digital automation. Some of them can only automate webapps, some can only automate keyboard + mouse. But this tool can do all, plus ability to automate using computer vision and extract info using OCR, all within one seamless API - https://github.com/tebelorg/TagUI-Python#use-cases

I've not prepared a table for comparison on the readme because I don't think the time is ripe to accelerate distribution of this project yet. It is better to grow organically to solve fundamental issues such as #14 #18 before user base expands quickly and a large mass of users having a negative first experience with the package. So far the pace is good, estimated 5k downloads in < 1 month.

Looking forward to listen to feedback from users and address more fundamental roadblocks! With the goal of encouraging porting over the project to other programming languages by other developers.

TagUI for Python downloads

PS - keep issue open for now for sharing with other users 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants