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
Comments
As TagUI-Python is running within a native Python script, you can re-organize the code with functions as follows: Sample Code 3 - using functionsfrom 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() |
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 With TagUI-Python, you can now maintain libraries of functions across multiple modules! Sample Code 4a - main filefrom 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.pyimport 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) |
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 fileimport 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.pyimport 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') |
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. PS - keep issue open for now for sharing with other users 👍 |
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
Sample Code 2 - in TagUI-Python
In comparison, the following is the same robot re-written using TagUI-Python
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.The text was updated successfully, but these errors were encountered: