Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. import pandas as pd
  2. import tkinter as tk
  3. import numpy as np
  4. from tabulate import tabulate
  5.  
  6. phones = pd.read_csv('data.csv')
  7. phones.drop(columns=['ImageUrl',"Unnamed: 0"],inplace = True)
  8.  
  9.  
  10. pd.options.display.max_columns = None
  11. pd.set_option('display.max_rows', 20)
  12. pd.set_option('display.max_columns', 50)
  13. pd.set_option('display.width', 100)
  14.  
  15. phones.index.name = 'ID'
  16. root = tk.Tk()
  17. v = tk.StringVar()
  18. columns = list(phones)
  19.  
  20.  
  21. #Input validation for the comparison function
  22. def inputValid(num,maxx):
  23.     validInput = False
  24.     while validInput == False:
  25.         try:    
  26.             num = int(input('Input ID '+num+': '))
  27.             if num > 0 and num < maxx:
  28.                 return num
  29.                 validInput = True
  30.             else:
  31.                 print('Invalid ID number.')
  32.         except ValueError:
  33.             print('Invalid entry.')
  34.  
  35. #Function which will display the table in a text window
  36. def textBox(x, h, w):
  37.    
  38.     xscrollbar = tk.Scrollbar(root, orient="horizontal")
  39.     yscrollbar = tk.Scrollbar(root)
  40.     xscrollbar.pack(side="bottom", fill="x")
  41.     yscrollbar.pack(side="right", fill="y")
  42.     T = tk.Text(root, height=h, width=w, xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
  43.     T.pack()
  44.     T.insert(tk.END,tabulate(x,headers='keys',tablefmt='psql'))
  45.     xscrollbar.config(command=T.xview)
  46.     yscrollbar.config(command=T.yview)
  47.     T.config(wrap="none")
  48.     tk.mainloop()
  49.  
  50. #Comparison function, asks user for two IDs and will display both in a text window
  51. def compare():
  52.     phone1 = inputValid('1',1151)
  53.     phone2 = inputValid('2',1151)
  54.     temp = phones.query('ID=='+str(phone1)+' or ID=='+str(phone2))
  55.     textBox(temp, 15, 200)
  56.  
  57. def sort():
  58.     valid = False
  59.     while valid == False:
  60.         sort = input("Enter column to sort by: ")
  61.         if sort in columns:
  62.             valid = True
  63.         else:
  64.             print("Invalid column name, please try again")
  65.     phones.sort_values(by=[sort], inplace=True)
  66.     sorts = phones[["Name", sort]]
  67.     textBox(sorts, 50, 150)
  68.  
  69. def addFilter():
  70.     x = v.get()
  71.     filtered = phones
  72.     print(filtered)
  73.     if type(phones[x][0]) is np.int64 or type(phones[x][0]) is np.float64:
  74.         value = input("Enter minimum value to be filtered by: ")
  75.         filtered = filtered.query(x+">"+value)
  76.     elif type(phones[x][0]) is str:
  77.         value = input("Enter value to be filtered by: ")
  78.         print(value)
  79.         filtered = filtered[filtered[x] == str(value)]
  80.     elif type(phones[x][0]) is np.bool_:
  81.         value = input("Filter by true or false: ")
  82.         filtered = filtered.query(x+"=="+value)
  83.     textBox(filtered, 50, 150)
  84.  
  85. def filtering():
  86.     values = {"Cost" : "Cost",
  87.               "Multi Camera" : "multiCamera",
  88.               "Rear Camera" : "rearCamera",
  89.               "Front Camera" : "frontCamera",
  90.               "Size (cm)" : "size_cm",
  91.               "Size (inches)" : "size_inch",
  92.               "Battery Capacity" : "batterySize",
  93.               "Battery Type" : "batteryType",
  94.               "RAM" : "RAM",
  95.               "ROM" : "ROM",
  96.               "Expandable Storage" : "Expandable"}
  97.    
  98.     for text, value in values.items():
  99.         b = tk.Radiobutton(root, text=text, variable=v, value=value).pack(anchor="w")
  100.     add_filter = tk.Button(root, text="Add Filter", command=addFilter).pack()
  101.    
  102.     tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement