Create a portable version of the desktop app in PyQt5 - python

I have made a UI in QtCreator 5. Then, I converted UI-file "Odor.ui" into "ui-main.py". Then I used Anaconda framework to write a computational part in Upiter Notebook. When, I compile this code
import os
import sys
import pandas as pd
import numpy
import scipy.stats
import xlsxwriter
import re
from PyQt5.QtWidgets import QApplication, QMainWindow, qApp, QWidget, QInputDialog, QLineEdit, QFileDialog, QMessageBox, QTableWidgetItem, QHeaderView
from PyQt5.QtCore import Qt, QEvent, QObject
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtGui
from ui_main import Ui_MainWindow
from os import walk
from os.path import expanduser as ospath
import glob
from time import gmtime, strftime
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
qApp.installEventFilter(self)
self.setupUi(self)
self.dataChooseBtn.clicked.connect(self.selectFile)
self.saveSample_btn.clicked.connect(self.saveSample)
self.data_processing.clicked.connect(self.process)
self.string_processing.clicked.connect(self.processString)
self.saveSampleString.clicked.connect(self.saveSampleStr)
self.refreshSample()
self.refreshSamples.clicked.connect(self.refreshSample)
self.refreshSample_2()
self.refreshSamples_2.clicked.connect(self.refreshSample_2)
self.show()
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress:
if event.key() == Qt.Key_Escape:
self.close()
return super(MyMainWindow, self).eventFilter(obj, event)
#pyqtSlot()
def accept(self):
textboxValue = self.paramInput_field_2.text()
QMessageBox.information(self, 'Message', "Значения параметрического столбца: " + textboxValue, QMessageBox.Ok, QMessageBox.Ok)
def selectFile(self):
self.fileName = None
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"Выберите стандартизированную выборку", "./Unprocessed samples","All Files (*);;Python Files (*.py)", options=options)
if fileName:
self.fileName = fileName
def process(self):
sample_param = pd.read_excel(self.fileName, header = None, sheetname = 1)
param = sample_param[0].tolist()
sample = pd.read_excel(self.fileName, sheetname = 0)
list_of_index = []
for i in range(len(sample.columns)):
sample2 = sample.iloc[:, lambda sample: [i]]
sample2 = sample2.columns[0]
list_of_index.append(sample2)
list_of_index
fulllist = []
for i in list_of_index:
sample3 = sample[i].tolist()
fulllist.append(sample3)
fulllist_percent = []
column_percent = []
len(fulllist)
for i in range(len(fulllist)):
for j in range(len(fulllist[i])):
percent_rank = scipy.stats.percentileofscore(fulllist[i], fulllist[i][j])
column_percent.append(percent_rank)
fulllist_percent.append(column_percent)
column_percent = []
fulllist_rank = []
for i in range(len(fulllist)):
rank = len(fulllist[i]) - scipy.stats.rankdata(fulllist[i]) + 1
fulllist_rank.append(rank)
param_rank = scipy.stats.rankdata(param).astype(int)
column_corr = []
for i in range(len(fulllist)):
correlation = scipy.stats.spearmanr(param_rank[::-1], fulllist_rank[i])
column_corr.append(correlation[0])
fulllist_click = []
for j in range (len(fulllist_percent)):
middle = []
if column_corr[j] > 0:
for i in range(len(fulllist_percent[j])):
solve = column_corr[j] * fulllist_percent[j][i]
middle.append(solve)
else:
for i in range (len(fulllist_percent[j])):
solve = abs(column_corr[j]) * (100 - fulllist_percent[j][i])
middle.append(solve)
fulllist_click.append(middle)
list_of_rowsumm = []
rowsumm = 0
fulllist_clickT = numpy.asarray(fulllist_click).T.tolist()
for i in range(len(fulllist_clickT)):
rowsumm = sum(fulllist_clickT[i])
list_of_rowsumm.append(rowsumm)
percent_rowsumm = []
for i in list_of_rowsumm:
x = scipy.stats.percentileofscore(list_of_rowsumm, i)
percent_rowsumm.append(x)
validity = scipy.stats.pearsonr(list_of_rowsumm, param)
validity = validity[0]
additional_info = []
additional_info.append(list_of_rowsumm)
additional_info.append(percent_rowsumm)
additional_info.append(param)
self.fulllist = fulllist
self.fulllist_percent = fulllist_percent
self.fulllist_click = fulllist_click
self.additional_info = additional_info
self.validity = validity
return self.fulllist, self.fulllist_percent, self.fulllist_click, self.additional_info, self.validity
def saveSample(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileNameSave, _ = QFileDialog.getSaveFileName(self,"Сохранить данные выборки","./Samples","Excel files (*.xlsx)", options=options)
workbook = xlsxwriter.Workbook(fileNameSave + ' ['+ str(round(self.validity, 3)) + ', ' + str(len(self.fulllist)) + ', ' + str(len(self.fulllist[0])) + ']' + ".xlsx")
worksheet1 = workbook.add_worksheet()
worksheet2 = workbook.add_worksheet()
worksheet3 = workbook.add_worksheet()
worksheet4 = workbook.add_worksheet()
row = 0
for col, data in enumerate(self.fulllist):
worksheet1.write_column(row, col, data)
for col, data in enumerate(self.fulllist_percent):
worksheet2.write_column(row, col, data)
for col, data in enumerate(self.fulllist_click):
worksheet3.write_column(row, col, data)
for col, data in enumerate(self.additional_info):
worksheet4.write_column(row, col, data)
workbook.close()
def processString(self):
check = self.stringInput_field.toPlainText()
check = [float(i) for i in check.replace(',', '.').split()]
index = self.sampleChoose_list.selectedIndexes()[0].row()
sample_path = self.sample_directory[1][index]
sample_param = pd.read_excel(ospath(sample_path), header = None, sheetname = 3)
param = sample_param[2].tolist()
param_rank = scipy.stats.rankdata(param).astype(int)
sample_2 = pd.read_excel(ospath(sample_path), header = None, sheetname = 0)
fulllist_new = []
for i in range(len(sample_2.columns)):
column_new = sample_2[i].tolist()
fulllist_new.append(column_new)
for i in range(len(check)):
fulllist_new[i][0] = check[i]
fulllist_percent_new = []
column_percent_new = []
for i in range(len(fulllist_new)):
for j in range(len(fulllist_new[i])):
percent_rank = scipy.stats.percentileofscore(fulllist_new[i], fulllist_new[i][j])
column_percent_new.append(percent_rank)
fulllist_percent_new.append(column_percent_new)
column_percent_new = []
fulllist_rank_new = []
for i in range(len(fulllist_new)):
rank = len(fulllist_new[i]) - scipy.stats.rankdata(fulllist_new[i]) + 1
fulllist_rank_new.append(rank)
column_corr_new = []
for i in range(len(fulllist_new)):
correlation = scipy.stats.spearmanr(param_rank[::-1], fulllist_rank_new[i])
column_corr_new.append(correlation[0])
fulllist_click_new = []
for j in range (len(fulllist_percent_new)):
middle = []
if column_corr_new[j] > 0:
for i in range(len(fulllist_percent_new[j])):
solve = column_corr_new[j] * fulllist_percent_new[j][i]
middle.append(solve)
else:
for i in range (len(fulllist_percent_new[j])):
solve = abs(column_corr_new[j]) * (100 - fulllist_percent_new[j][i])
middle.append(solve)
fulllist_click_new.append(middle)
check_click = []
for i in range(len(check)):
if column_corr_new[i] > 0:
click = fulllist_percent_new[i][0] * column_corr_new[i]
check_click.append(click)
elif column_corr_new[i] < 0:
click = abs(column_corr_new[i]) * (100 - fulllist_percent_new[i][0])
check_click.append(click)
len(check_click)
list_of_rowsumm_new = []
rowsumm = 0
fulllist_click_newT = numpy.asarray(fulllist_click_new).T.tolist()
for i in range(len(fulllist_click_newT)):
rowsumm = sum(fulllist_click_newT[i])
list_of_rowsumm_new.append(rowsumm)
percent_rowsumm_new = []
for i in list_of_rowsumm_new:
x = scipy.stats.percentileofscore(list_of_rowsumm_new, i)
percent_rowsumm_new.append(x)
validity_new = scipy.stats.pearsonr(list_of_rowsumm_new[1:], param[1:])
validity_new = validity_new[0]
validity_list = []
validity_list.append(validity_new)
additional_info_new = []
additional_info_new.append(list_of_rowsumm_new)
additional_info_new.append(percent_rowsumm_new)
additional_info_new.append(validity_list)
additional_info_new.append(param)
self.fulllist_new = fulllist_new
self.fulllist_percent_new = fulllist_percent_new
self.fulllist_click_new = fulllist_click_new
self.additional_info_new = additional_info_new
self.validity_new = validity_new
return self.fulllist_new, self.fulllist_percent_new, self.fulllist_click_new, self.additional_info_new, self.validity_new
def saveSampleStr(self):
time = strftime("%d:%m:%Y %H-%M", gmtime())
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileNameSave, _ = QFileDialog.getSaveFileName(self,"Сохранить обработанную строку","./Processed","Excel files (*.xlsx)", options=options)
workbook = xlsxwriter.Workbook(fileNameSave + ' (' + time + ')' + ".xlsx")
worksheet1 = workbook.add_worksheet()
worksheet2 = workbook.add_worksheet()
worksheet3 = workbook.add_worksheet()
worksheet4 = workbook.add_worksheet()
row = 0
for col, data in enumerate(self.fulllist_new):
worksheet1.write_column(row, col, data)
for col, data in enumerate(self.fulllist_percent_new):
worksheet2.write_column(row, col, data)
for col, data in enumerate(self.fulllist_click_new):
worksheet3.write_column(row, col, data)
for col, data in enumerate(self.additional_info_new):
worksheet4.write_column(row, col, data)
workbook.close()
def refreshSample(self):
sample_directory = []
sample_files = []
for (dirpath, dirnames, filenames) in walk('./Samples'):
filenames = [f for f in filenames if not f[0] == '.']
sample_files.extend(filenames)
break
the_dir = "Samples"
paths = [os.path.abspath(os.path.join(the_dir,filename)) for filename in os.listdir(the_dir) if not filename.startswith('.')]
sample_directory.append(sample_files)
sample_directory.append(paths)
self.sample_directory = sample_directory
self.sampleChoose_list.clear()
self.sampleChoose_list.addItems(sample_directory[0])
self.sampleChoose_list.setSortingEnabled(True);
self.sampleChoose_list.sortItems()
return self.sample_directory
def refreshSample_2(self):
sample_directory_2 = []
sample_files_2 = []
for (dirpath, dirnames, filenames) in walk('./Processed'):
filenames = [f for f in filenames if not f[0] == '.']
sample_files_2.extend(filenames)
break
the_dir = "Processed"
paths_2 = [os.path.abspath(os.path.join(the_dir,filename)) for filename in os.listdir(the_dir) if not filename.startswith('.')]
sample_directory_2.append(sample_files_2)
sample_directory_2.append(paths_2)
processed_info = []
for i in range(len(sample_directory_2[0])):
file_info = []
sample_file_2 = sample_directory_2[0][i]
sample_path_2 = sample_directory_2[1][i]
sample_info_2 = pd.read_excel(ospath(sample_path_2), header = None, sheetname = 3)
sample_info_2 = sample_info_2.iloc[0][0:3]
file_info.append(sample_file_2)
sample_info_2_list = numpy.array(sample_info_2).tolist()
file_info.extend(sample_info_2_list)
processed_info.append(file_info)
self.clickSample_list.setRowCount(len(processed_info))
self.clickSample_list.setColumnCount(4)
labels = ['Имя', 'Массовые отклики', 'Процентранг, %', 'Валидность']
self.clickSample_list.setHorizontalHeaderLabels(labels)
red = QtGui.QColor(255, 5, 5);
orange = QtGui.QColor(255, 157, 0);
blue = QtGui.QColor(0, 46, 255);
for row in range(len(processed_info)):
for column in range(len(processed_info[row])):
self.clickSample_list.setItem(row, column, QTableWidgetItem(str(processed_info[row][column])))
if column == 2:
if processed_info[row][column] > 85:
color = red;
if processed_info[row][column] > 65 and processed_info[row][column] < 85:
color = orange;
if processed_info[row][column] < 65:
color = blue;
self.clickSample_list.item(row, column).setBackground(color);
self.clickSample_list.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyMainWindow()
sys.exit(app.exec_())
my app is launched in a window and UI is loaded by from ui_main import Ui_MainWindow. My question is, how to make a desktop app? Do I need to do it in special Qt5 creator files? Or is possible to make it out of .ipynb-file from Anaconda? I am trying to make both MacOs and Windows versions, but I am pretty new to app-building and programming and do not know, how to begin.

You can use cx_Freeze to create a desktop app from a python program.
There's a guide to packaging a PyQt application:
cxfreeze-quickstart # generates a creation script
On OSX, you have the option of building a .dmg or a .app, by executing one of these at the prompt:
python setup.py bdist_dmg
python setup.py bdist_mac
On Windows:
python setup.py bdist_msi
There's a comparison of deployment tools here.

Related

Open File Dialog running twice repeatedly

I tried to open two different types of files with QFileDialog in PyQt5 for pandas dataframe but it open repeatedly twice for each files before executed. However, when I'm using specific file only 'filename' it will directly executed once.
It's opened twice using this code:
class dataframe(QWidget):
def __init__(self, parent=None):
super(dataframe, self).__init__(parent)
layout = QVBoxLayout(self)
canvas = mycanvas(self)
layout.addWidget(canvas)
self.btn3 = QPushButton("Plot")
self.btn3.clicked.connect(canvas.PlotChart)
layout.addWidget(self.btn3)
def input(self):
lasdata = QFileDialog.getOpenFileName(self, 'Open LAS', r'data/', "LAS files (*.las)")[0]
textdata = QFileDialog.getOpenFileName(self, 'Open Text', r'data/', "Text files (*.txt)")[0]
l = lasio.read(f"{lasdata}")
data = l.df()
data = data.replace('-999.00000', np.nan)
data.index.names = ['DEPT']
w = l.well.WELL.value
data['WELL'] = w
text = pd.read_csv(f"{textdata}", sep='\t')
text_unit = text['RU'].unique()
data_w = pd.DataFrame()
for i in range(len(text_unit)):
top = text.iloc[i]['D']
if i < len(text_unit) - 1:
bottom = text.iloc[i + 1]['D']
else:
bottom = int(round(data.tail(1).index.item()))
data_int = data.iloc[top:bottom, :]
data_int['INT'] = text.iloc[i]['RU']
data_w = data_w.append(data_int)
data = data_w
return w, data
def det(self, gr):
w, data = dataframe.input(self)
GR = data.iloc[:, gr]
conditions = [
(GR <= 60),
(GR >= 60)]
lit = ['A', 'B']
data['L'] = np.select(conditions, lit, default='Undefined')
return data
def calculation(self):
w, data = dataframe.input(self)
data = dataframe.det(self, 1)
data_well = pd.DataFrame()
data_int = pd.DataFrame()
F_w = pd.DataFrame()
Fa = pd.DataFrame()
text_unit = data['INT'].unique()
for i in range(len(text_unit)):
data_int = data.where(data['INT'] == text_unit[i]).dropna()
F_percent = data_int['L'].value_counts(normalize=True) * 100
F_percent = F_percent.sort_index()
F_percent['INT'] = text_unit[i]
F_percent = pd.DataFrame(F_percent).transpose()
F_w = F_w.append(F_percent)
F_w['W'] = w
Fa = Fa.append(F_w)
F_w = pd.DataFrame()
Fa = Fa.reset_index()
Fa = Fa.fillna(0)
return Fa
class mycanvas(FigureCanvas):
def __init__(self, parent=None, dpi=120):
self.fig = plt.Figure(dpi=dpi)
FigureCanvas.__init__(self, self.fig)
def PlotChart(self):
outputs = dataframe.calculation(self) # .where(Fa['W']==well)
int = outputs['INT'].unique()
self.axes = self.fig.add_subplot(111)
self.axes.set_title("AA")
outputs.plot.barh(x='INT', stacked=True, ax=self.axes)
self.axes.set_yticks(range(len('INT')), 'INT')
self.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = dataframe()
window.show()
sys.exit(app.exec_())
However, it's directly executed once with this code:
def Input(self):
lasdata = 'data/AA.las'
textdata = 'data/AATEXT.txt'
l = lasio.read(lasdata)
data = l.df()
text = pd.read_csv(textdata, sep='\t')
Is there something wrong with File Dialog code, as with the lower code (direct name) it successfully executed once, but with the file dialog it opened twice before read the next codes. I need to make the GUI app so I need to use the file dialog.
This is part of the code with the two buttons:
self.btn1 = QPushButton("LAS File")
self.btn1.clicked.connect(self.las)
self.btn2 = QPushButton("Text File")
self.btn2.clicked.connect(self.text)
self.btn3 = QPushButton("Plot")
self.btn3.clicked.connect(canvas.PlotChart)
layout.addWidget(self.btn1)
layout.addWidget(self.btn2)
layout.addWidget(self.btn3)
def las(self):
self.lasfile = QFileDialog.getOpenFileName(self, 'Open LAS', r'data/', "LAS files (*.las)")[0]
def text(self):
self.textfile = QFileDialog.getOpenFileName(self, 'Open Text', r'data/', "Text files (*.txt)")[0]
def input(self):
lasdata = self.lasfile
textdata = self.textfile
l = lasio.read(f"{lasdata}")
data = l.df()
data = data.replace('-999.00000', np.nan)
data.index.names = ['DEPT']
w = l.well.WELL.value
data['WELL'] = w
text = pd.read_csv(f"{textdata}", sep='\t')
text_unit = text['RU'].unique()
I try to make it into two buttons but it can't be opened or exit fail to be executed.
below is one of the text format
WN RU D
AA SR 19
AA TF 50
AA PSU 1700

How to show text variable in the Tkinter window?

I'm working on thermography camera and i making the interface. I face some error that my textvariable won't print in the tkinter but still can be printed on the shell. What is wrong with my code?
import pandas as pd
import cv2
import matplotlib.pyplot as plt
from tkinter import *
from tkinter.filedialog import askopenfilenames
# List of list
images = []
gs = []
B = []
G = []
R = []
suhu = []
suhuo = []
red = []
green = []
blue = []
waktu = []
utama = Tk()
# Fungsi-fungsi
def mouseRGB(event,x,y,flags,param):
global gambar
if event == cv2.EVENT_LBUTTONDOWN:
colorsB = gambar[y,x,0]
colorsG = gambar[y,x,1]
colorsR = gambar[y,x,2]
R.append(colorsR)
G.append(colorsG)
B.append(colorsB)
def Tutup ():
utama.destroy()
def tolong ():
messagebox.showinfo('Help','Jika terjadi error atau kesulitan dalam menggunakan program ini dapat menghubungi 08111903747')
def loadImages ():
global wkt
global gambar
global tem
wkt = float(wktu.get())
Gambar = askopenfilenames(parent=jwaktu,
title='Select a thermal image file',
filetypes=[('JPG File','*.jpg'),
('PNG File','*.png'),
('All Files','*')])
# Tutorial pake aplikasi
messagebox.showinfo('How to use','After clicking or dragging the selected picture, press S for next image')
# Program olah suhu
# Perintah Load gambar ke python
filenames = Gambar
# Jendela Mouse
cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)
time = 0
for file in filenames:
gambar = cv2.imread(file)
time += wkt
cv2.imshow('mouseRGB',gambar)
while(1):
if cv2.waitKey(0) & 0xFF == ord('s') :
for r in R:
red.append(float(r))
for g in G:
green.append(float(g))
for b in B:
blue.append(float(b))
nr = [i * 0.020411398053616 for i in red]
ng = [j * -0.01805397732321 for j in green]
nb = [k * -0.06212844019856 for k in blue]
R.clear()
G.clear()
B.clear()
red.clear()
green.clear()
blue.clear()
lum = nr + ng + nb
lumi = sum(lum)
tem = lumi + 34.9927907296913
if tem >= 33.0 and tem<= 39.0:
suhuo.append(tem)
waktu.append(time)
suhu.append([file,time,tem])
SuhuA = Label(jwaktu,textvariable = tem)
SuhuA.grid(column=1, row = 1)
print(tem)
break
cv2.destroyAllWindows()
# Olah grafik
plt.plot(waktu,suhuo)
plt.xlabel('Waktu (s)')
plt.ylabel('Suhu (C)')
plt.title('Sebaran suhu')
plt.savefig('Sebaran_suhu.png')
plt.show()
# Masukan data kedalam csv
img_df = pd.DataFrame(suhu ,columns = ['Image','Waktu','Temperature'])
img_df.to_csv('Olah_Suhu.csv')
waktu.clear()
suhuo.clear()
def TIP ():
global SuhuA
global wktu
global jwaktu
jwaktu = Tk()
jwaktu.title('Thermal Image Processing')
jwaktu.geometry('600x400')
Tulisanw = Label(jwaktu,text='Enter the sampling time : ')
Tulisanw.grid(column = 0 , row = 0)
wktu = Entry(jwaktu,width = 10)
wktu.grid(column = 1, row = 0)
tombolw = Button(jwaktu,text='Execute',command=loadImages)
tombolw.grid(column = 2, row = 0)
Suhu = Label(jwaktu,text='Temperature Point : ')
Suhu.grid(column = 0, row = 1)
jwaktu.mainloop()
return wktu
# Window GUI
utama.title('TCI - Thermograpgy Camera Interface')
utama.geometry('600x400')
menu = Menu(utama)
program = Menu(menu)
program.add_command(label = 'Close',command=Tutup)
menu.add_cascade(label='File', menu=program)
menu.add_command(label = 'Help',command=tolong)
utama.config(menu=menu)
TombolTIP = Button(utama,width=30,text='Thermal Image Processing',command=TIP)
TombolTIP.grid(padx = 200,pady = 50)
TombolRTM = Button(utama,width=30,text = 'Realtime Thermal Measurment')
TombolRTM.grid(padx=200,pady=150)
utama.mainloop()
When i was executing on the other window the temperature point did not show anything. I have already tried to use 'text' instead of 'textvariable', but nothing happened. Thank you for your help!

How to import values from excel with pandas into tkinter faster?

guys! How are you? I have this code below and I'm having this trouble with the insertData function. This function is used to enter values from an excel spreadsheet into the tkinter treeview using Pandas. It's almost all right, but it's too slow and I don't know how to fix it. If anyone could help me, I'd be very happy.
Thanks in advance!
from tkinter import *
import ttk
import openpyxl
import pandas as pd
nuScreen = Tk()
nuScreen.title("ContrasinSystem - Usuário Comum")
nuScreen.iconbitmap("logocontransin.ico")
book = openpyxl.load_workbook('Registros.xlsx')
sheet = book.sheetnames
sh = book.active.cell
#Contador de Linhas:
wb2 = openpyxl.load_workbook('Registros.xlsx')
sheet2 = wb2.worksheets[0]
rowCount = sheet2.max_row
v = []
#Design:
class main():
def __init__(self,tk):
for x in range (0,len(sheet)):
v.append(sheet[int(x)])
self.wb2 = openpyxl.load_workbook('Registros.xlsx')
self.sheet2 = self.wb2.worksheets[0]
self.row_count = self.sheet2.max_row
self.column_count = self.sheet2.max_column
self.nuFrame = Frame(nuScreen, width = 1500, height = 450)
self.nuFrame.pack()
self.img = PhotoImage(file="logocontransin.png")
self.w = Label(self.nuFrame, image = self.img)
self.w.img = self.img
self.w.place(x=65,y=150)
self.srchlbl = ttk.Label(self.nuFrame, text = "Buscar:")
self.srchlbl.place(x=25,y=75)
self.srchetr = ttk.Entry(self.nuFrame, width = 30)
self.srchetr.place(x=75,y=75)
self.treeview = ttk.Treeview(self.nuFrame)
self.treeview.place(x=300,y=75, width = 1100)
dataVector = []
def columnsName():
def Header():
self.columnVector = []
self.dataVector = []
teste = []
self.treeview.column("#0", width = 20)
self.columnHeader = pd.read_excel(r'Registros.xlsx', str(self.cmb.get()), header_only = True, nrows=0).columns
for a in self.columnHeader:
self.columnVector.append(a)
self.treeview.configure(columns = self.columnVector)
for b in self.columnHeader:
self.treeview.heading(str(b), text = str(b))
def insertData():
for m in range(rowCount):
self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), skip_blank_lines=True, skiprows=0)
for l in self.dataValues:
dataVector.append(l)
self.treeview.insert("", "end",values = dataVector)
print(self.dataValues)
Header()
insertData()
self.cmbLbl = ttk.Label(self.nuFrame, text = "Assunto:")
self.cmbLbl.place(x=1200, y=325)
self.cmb = ttk.Combobox(self.nuFrame, values = v)
self.cmb.place(x=1250,y=325)
self.btncmb = ttk.Button(self.nuFrame, text = "Buscar", command = columnsName)
self.btncmb.place(x=1320,y=375)
nuScreen.geometry("1500x450")
main(nuScreen)
nuScreen.mainloop()
How many times do you want to open a excel file for reading?
def insertData():
for m in range(rowCount):
self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()),
skip_blank_lines=True, skiprows=0)
for l in self.dataValues:
dataVector.append(l)
self.treeview.insert("", "end",values = dataVector)
print(self.dataValues)
Should you instead omit the first loop?
def insertData():
self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()),
skip_blank_lines=True, skiprows=0)
for l in self.dataValues:
self.treeview.insert("", "end",values = l)
print(self.dataValues)

TypeError: unbound method download_csv_file_web() must be called with csv_util instance as first argument (got str instance instead)

I am using Python to download stock data from Yahoo. The download code is placed in csv_util.py.
The invoking script is mapper.py. After importing csv_util script, I get the following error:
Traceback (most recent call last):
('Lines:', [])
File "C:/Users/laurel.ts/Desktop/predictstock/mapper.py", line 56, in <module>
download_file_ticker(ticker,ref_ticker)
File "C:/Users/laurel.ts/Desktop/predictstock/mapper.py", line 53, in download_file_ticker
csv_util.download_csv_file_web(ticker,ref_ticker)
TypeError: unbound method download_csv_file_web() must be called with csv_util instance as first argument (got str instance instead)
Here is the code:
mapper.py
###### Mapper.py ######
import time
import sys
# New imports
import stock_predict_main_app
import predict_stock.csv_util
from predict_stock.csv_util import csv_util
predict_date = '03112016'
ticker = 'ARVIND.NS'
ref_ticker = 'MSFT'
input_default_values = {'numdays2predict': 2,
'simulations': 10,
'historicaldatalen': 0,
'tickersymbol': 'ARVIND.NS',
# 'tickersymbol': 'MSFT',
'stockdate2predict': predict_date,
'downloadstock': 1,
'plotshow': 0,
'industrytype': 'itindustry'}
# Pass ticker symbol and date
def pass_ticker_date(predict_date, input_default_values):
with open('tickerList.txt') as f:
lines = f.read().splitlines()
print(type(input_default_values))
tickersymbol = input_default_values["tickersymbol"]
print("Lines:", lines)
for tickersymbol in lines:
print("tickersymbol:", tickersymbol)
stock_predict_main_app.test_predict(tickersymbol)
# Download the file based on the ticker symbol
def download_file_ticker(ticker,ref_ticker):
# ticker= input_default_values["tickersymbol"]
# ref_ticker = input_default_values["tickersymbol"]
csv_util.download_csv_file_web(ticker,ref_ticker)
pass_ticker_date(predict_date, input_default_values)
download_file_ticker(ticker,ref_ticker)
csv_util.py
import logging
import csv
import urllib
import datetime
import numpy as np
import pandas as pd
import sys
import os
from collections import defaultdict
###custom local modules###
from datetime_util import datetime_util
from loggingpy import loggingpy
global stock_input_file,stock_input_ref_file
global loggingpy_obj,datetime_util_obj
global actual_stock_price, output_stock_price
class csv_util:
def __init__(self,actual_stock_price,output_stock_price,download_flag):
print("Class anme: __init__",self.__class__.__name__)
self.stock_input_file=""
self.stock_input_ref_file = ""
self.actual_stock_price = actual_stock_price
self.output_stock_price = output_stock_price
self.download_flag=download_flag
self.datetime_util_obj = datetime_util()
self.loggingpy_obj = loggingpy()
#datetime_util_obj = self.datetime_util_obj
#loggingpy_obj=self.loggingpy_obj
'''
METHOD: prepare_actual_data2writecsv
'''
def prepare_actual_data2writecsv(self, predict_date_wise_data_wd, predict_datewise_data_wod):
logging.info("<<prepare_actual_data2writecsv")
temp_date_ary = []
temp_date_ary = self.actual_stock_price['date']
temp_closeprice_ary = self.actual_stock_price['closeprice']
temp_dailyreturn_ary = self.actual_stock_price['dailyreturn']
# predicted_date_array = sorted(temp_predicted_values_wd.keys(),reverse=True)
# remove last element of array or appenda dummy 0 to daily returns
temp_date_ary.pop()
temp_closeprice_ary.pop()
# temp_dailyreturn_ary.append(0)
self.loggingpy_obj.log_func({'temp_date_ary': temp_date_ary, 'temp_closeprice_ary': temp_closeprice_ary,
'temp_dailyreturn_ary': temp_dailyreturn_ary})
np_column_ary = np.column_stack((temp_date_ary, temp_closeprice_ary, temp_dailyreturn_ary))
num_rows, num_columns = np_column_ary.shape
logging.info("np_column_ary:%s,Rowsdata %s,ColumnData %s", np_column_ary.size, np_column_ary[:][0],
np_column_ary[:, 0])
logging.info("NumRows:%d,Num Columns:%s", num_rows, num_columns)
counter = 0
for i in range(0, num_rows):
counter += 1
temp_temp_row_data = []
temp_row_data = np_column_ary[:][i]
temp_temp_row_data = list(temp_row_data)
temp_rowdate = temp_row_data[0]
logging.debug("[%d],Length:[%d],type:[%s],Date:%s,Rowsdata:%s", i, len(temp_row_data), type(temp_row_data),
temp_rowdate, temp_row_data)
predict_date_wise_data_wd[temp_rowdate] = (map(str, temp_temp_row_data))
predict_datewise_data_wod[temp_rowdate] = (map(str, temp_temp_row_data))
logging.info(">>prepare_actual_data2writecsv")
'''
METHOD: prepare_data2writecsv
'''
def prep_predicted_data2writecsv(self, predict_date_wise_data_wd, predict_datewise_data_wod):
logging.info("<<prep_predicted_data2writecsv")
temp_predicted_values_wd = self.actual_stock_price['predicted_vals_with_drift']
temp_predicted_values_wod = self.actual_stock_price['predicted_vals_without_drift']
self.actual_stock_price['meanwithdrift'] = []
self.actual_stock_price['meanwithoutdrift'] = []
temp_var = temp_predicted_values_wd.keys()
predicted_date_array = self.datetime_util_obj.sort_datettime_list(temp_var, False)
for eack_key in predicted_date_array:
logging.debug("WD:eack key:%s", eack_key)
temp_string_val_wd = []
temp_string_val_wod = []
temp_string_val_wd = temp_predicted_values_wd[eack_key]
temp_string_val_wod = temp_predicted_values_wod[eack_key]
mean_wd = np.mean(temp_string_val_wd)
mean_wod = np.mean(temp_string_val_wod)
# Store mean in global variable
self.actual_stock_price['meanwithdrift'].append(mean_wd)
self.actual_stock_price['meanwithoutdrift'].append(mean_wod)
logging.debug("meanwithdrift:%s,meanwithoutdrift:%s", mean_wd, mean_wod)
logging.debug("temp_string_val_wd:len:%d,type:%s", len(temp_string_val_wd), type(temp_string_val_wd))
logging.debug("temp_string_val_wd:len:%d,type:%s", len(temp_string_val_wod), type(temp_string_val_wod))
temp_string_wd = []
temp_string_wod = []
if not predict_datewise_data_wod.has_key(eack_key):
predict_datewise_data_wod[eack_key] = []
predict_date_wise_data_wd[eack_key] = []
temp_string_wd = [eack_key, "", ""]
temp_string_wod = [eack_key, "", ""]
temp_string_wd.append(mean_wd)
temp_string_wod.append(mean_wod)
temp_string_wd.extend(temp_string_val_wd)
temp_string_wod.extend(temp_string_val_wod)
logging.debug("temp_string_wd:len:%d,type:%s,Data:%s", len(temp_string_wd), type(temp_string_wd),
temp_string_wd)
logging.debug("temp_string_wod:len:%d,type:%s,Data:%s", len(temp_string_wod), type(temp_string_wod),
temp_string_wod)
predict_date_wise_data_wd[eack_key].extend(temp_string_wd)
predict_datewise_data_wod[eack_key].extend(temp_string_wod)
#self.loggingpy_obj.log_func({"temp_string_wd": temp_string_wd, "temp_string_wod": temp_string_wod})
logging.info(">>prepare_data2writecsv")
'''
METHOD: write2csvfile
Writes given data to the given csv absolute path filename
Input arguments: filename to be written, data to be written
'''
def write2csvfile(self,file_name,local_rows_data):
logging.info("<<:write2csvfile")
#output_data_path=self.loggingpy_obj.output_data_path
#os.chdir(output_data_path)
with open(file_name, 'w') as csv_fw:
out_csv_writer = csv.writer(csv_fw, lineterminator='\n')
out_csv_writer.writerows(local_rows_data)
logging.info("csv file[%s]writing :Sucess",file_name)
logging.info(">>:write2csvfile")
'''
# Prepare header list of columns to write to csv file
# Write predicted values to csv file
predicted_data_wod_date_val_ary
'''
##classmethod
#def write2csv_file(ticker_symbol):
def write2csv_file(self,ticker_symbol):
logging.info("<<:write2csv_file")
datetime_stamp=datetime.datetime.now().strftime("%Y%m%d_%H%M")
file_name="output_prediction_with_drift"+ticker_symbol+"_"+datetime_stamp+".csv"
file_name_wod = "output_prediction_without_drift" + ticker_symbol + "_" + datetime_stamp + ".csv"
file_name=self.loggingpy_obj.output_data_path + file_name
file_name_wod = self.loggingpy_obj.output_data_path + file_name_wod
column_headers=self.output_stock_price['column_headers']
#Prepare header list of columns to write to csv file;column_headers is a global variable
column_headers.insert(0,'Date')
column_headers.insert(1, 'Actual Prices')
column_headers.insert(2, 'Daily Return')
column_headers.insert(3, 'Mean')
logging.info("column_headers,len:%s,type:%s,data:%s", len(column_headers), type(column_headers), column_headers)
logging.info("self:column_headers,len:%s", len(self.output_stock_price['column_headers']))
predict_date_wise_data_wd = {}
predict_datewise_data_wod = {}
self.prepare_actual_data2writecsv(predict_date_wise_data_wd, predict_datewise_data_wod)
self.loggingpy_obj.log_func(
{"Before:predict data_wd": predict_date_wise_data_wd, "predict data_wod": predict_datewise_data_wod})
self.prep_predicted_data2writecsv(predict_date_wise_data_wd,predict_datewise_data_wod)
self.loggingpy_obj.log_func({"After:pred data_wd": predict_date_wise_data_wd, "pred data_wod": predict_datewise_data_wod})
temp_new_data_ary=predict_date_wise_data_wd.keys()
sorted_temp_new_data_ary = self.datetime_util_obj.sort_datettime_list(temp_new_data_ary,True)
self.loggingpy_obj.log_func({"sorted_temp_new_data_ary":sorted_temp_new_data_ary})
data2write2csv_wd = [column_headers]
data2write2csv_wod = [column_headers]
counter=1
# add headers
for each_key in sorted_temp_new_data_ary:
counter+=1
data2write2csv_wd.insert(counter, predict_date_wise_data_wd[each_key])
data2write2csv_wod.insert(counter,predict_datewise_data_wod[each_key])
self.write2csvfile(file_name, data2write2csv_wd)
self.write2csvfile(file_name_wod, data2write2csv_wod)
logging.debug("data2write2csv_wd:%s", repr(data2write2csv_wd))
logging.info("<<:write2csv_file")
#sys.exit()
# ######################### END OF METHOD write2csv_file ################################
'''
METHOD: read_csv_file
'''
##classmethod
def read_csv_file(self,file_name,ref_data_flag):
logging.debug("<<:read_csv_file")
logging.info("file_name,%s,",file_name)
if not os.path.exists(file_name):
logging.critical("File not found:Check!"+file_name)
sys.exit(2)
#actual_stock_price=self.actual_stock_price
logging.info("file_name,%s,", file_name)
data = pd.read_csv(file_name,parse_dates=False, infer_datetime_format=False,date_parser=None)
logging.info("self:::::::::::::%s",repr(self.datetime_util_obj));
logging.info("data columns,len:%d,type:%s,data:%s,",len(data.columns),type(data.columns),data.columns)
logging.info(",data.columns:%s",data.columns.values)
datetime_list_dmy = self.datetime_util_obj.convert2datettime_list(data['Date'].tolist())
#actual_stock_price=defaultdict();
actual_stock_price = self.actual_stock_price
if ref_data_flag == 1:
actual_stock_price['ref_data']={}
actual_stock_price['ref_data']['date'] = datetime_list_dmy
actual_stock_price['ref_data']['closeprice'] = data['Close'].tolist()
else:
actual_stock_price['date'] = datetime_list_dmy
actual_stock_price['closeprice'] = data['Close'].tolist()
self.loggingpy_obj.log_func({"datetime_list_dmy": datetime_list_dmy})
#logging.debug("repr self asp:%s",repr(self.actual_stock_price))
del data
logging.debug(">>:read_csv_file")
'''
METHOD: download_csv_file_web
Download stock data from web yahoofinance
'''
def download_csv_file_web(self,ticker_symbol,ref_ticker_symbol):
logging.debug("<<:download_csv_file_web")
input_data_path=self.loggingpy_obj.input_data_path
logging.info("input_data_path:%s:",input_data_path)
yahoo_url="http://real-chart.finance.yahoo.com/table.csv?s="
base_url=yahoo_url + ticker_symbol
base_url_ref = yahoo_url + ref_ticker_symbol
#datetime_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M")
datetime_stamp = datetime.datetime.now().strftime("%Y%m%d")
global stock_input_file,stock_input_ref_file
#print("File found1:", stock_input_file)
if self.download_flag == 1:
file_name = "stock_input_" + ticker_symbol + "_" + datetime_stamp + ".csv"
ref_file_name = "stock_ref_input_" + ref_ticker_symbol + "_" + datetime_stamp + ".csv"
stock_input_file = input_data_path + "\\" + file_name
stock_input_ref_file = input_data_path + "\\" + ref_file_name
self.download_file(stock_input_file,base_url)
self.download_file(stock_input_ref_file, base_url_ref)
else:
file_name = "stock_input_" + ticker_symbol + ".csv"
ref_file_name = "stock_ref_input_" + ref_ticker_symbol + ".csv"
stock_input_file = input_data_path + "\\" + file_name
stock_input_ref_file = input_data_path + "\\" + ref_file_name
if os.path.isfile(stock_input_file) and os.path.isfile(stock_input_ref_file):
logging.info("File found 3 :[%s],[%s]",stock_input_file,stock_input_ref_file)
print("File found3:",stock_input_file,stock_input_ref_file)
else:
print("File not found4:", stock_input_file,stock_input_ref_file)
logging.critical("File not found4![%s] or [%s]",stock_input_file,stock_input_ref_file)
sys.exit(2)
print("File found5:", stock_input_file,stock_input_ref_file)
logging.info("stock_input_file 5 :%s,base_url:%s,ref file name:[%s]", stock_input_file, base_url,stock_input_ref_file)
self.stock_input_file=stock_input_file
self.stock_input_ref_file=stock_input_ref_file
#sys.exit(2)
logging.debug(">>:download_csv_file_web")
'''
METHOD: download_file
Downlaod stock data from web yahoofinance
'''
def download_file(self,file_name,base_url):
logging.debug("<<:download_file")
try:
logging.info("Try Reading [:%s]",base_url)
status = urllib.urlretrieve(base_url, file_name)
logging.info("Status:%s", status)
urllib.urlcleanup()
if os.path.exists(file_name):
logging.info ("File exists, File download success!File"+file_name)
else:
logging.critical("File downloaded DOE NOT EXIST, exit.."+file_name)
sys.exit(2)
except urllib.ContentTooShortError as ctse:
print("File download: Failed, found some error")
logging.critical("File donwlaod failed from url:%s",base_url)
sys.exit(2)
#logfile_handle.write(ctse.content)
logging.debug(">>:download_file")
def download_read_csv_file(self,ticker_symbol, industry_type_ts):
logging.debug("<<:download_read_csv_file")
ref_data_flag=0
self.download_csv_file_web(ticker_symbol, industry_type_ts)
stock_input_file = self.stock_input_file
stock_input_ref_file = self.stock_input_ref_file
# download_csv_file_web("GLD")
ref_data_flag = 0
self.read_csv_file(stock_input_file, 0)
self.read_csv_file(stock_input_ref_file, 1)
#self.loggingpy_obj.log_func({"csv_util:actual_stock_price":self.actual_stock_price})
logging.debug(">>:download_read_csv_file")
loggingpy.py
import logging
import datetime
import os
import sys
global current_dir_path, input_data_path, output_data_path
class loggingpy:
def __init__(self):
loglevel=logging.INFO
self.loglevel=loglevel
self.log_config()
print("loggingpy - log __init__")
def log_config(self):
global current_dir_path, input_data_path, output_data_path
current_dir_path = os.getcwd()
input_data_path = current_dir_path + "\..\stock_data"
output_data_path = current_dir_path + "\..\stock_data"
if not os.path.exists(output_data_path):
os.mkdir(output_data_path)
print("current_dir_path:" + current_dir_path + ",input_data_path:" + input_data_path,
",output_data_path:" + output_data_path)
main_date_time_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M")
logfile = output_data_path + "\log_" + main_date_time_stamp + ".log"
#logging.basicConfig(stream=sys.stdout,level=logging.DEBUG, format='%(levelname)s:%(message)s')
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
#logging.basicConfig(stream=sys.stdout, level=self.loglevel)
#logging.basicConfig(filename=logfile, level=logging.INFO, format='%(levelname)s:%(message)s')
#console=logging.StreamHandler().setLevel(logging.DEBUG)
#logging.getLogger('abcd').addHandler(console)
self.input_data_path = input_data_path
self.output_data_path = output_data_path
self.current_dir_path = current_dir_path
logging.info("Logging test %s","HELLO TEST")
logging.info("current_dir_path:%s,input_data_path:%s,output_data_path:%s", current_dir_path,input_data_path,output_data_path)
def log_fn(self,temp_list):
for i in range(0, len(temp_list)):
log_level_set=logging.getLogger().getEffectiveLevel()
#print("log_level_set",log_level_set,logging.INFO,logging.DEBUG)
if log_level_set==logging.INFO:
logging.info(":len:%d,Type:%s", len(temp_list[i]), type(temp_list[i]))
if log_level_set == logging.DEBUG:
logging.debug("len:%d,Type:%sData:%s", len(temp_list[i]), type(temp_list[i]),temp_list[i])
def log_info(self,msg):
logging.info(msg)
def log_func(self,templog):
log_level_set = logging.getLogger().getEffectiveLevel()
#log_string_info = "Name:%s,len:%d,type:%s"
#log_string_debug = "Name:%s,len:%d,type:%s,Data:%s"
log_string_info = "Name:{0},len:{1},type:{2}"
log_string_debug = "Name:{0},len:{1},type:{2},Data:%{3}"
for var_name,var_value in templog.iteritems():
if log_level_set == logging.INFO:
#logging.info(""+log_string_info,var_name, len(var_value),type(var_value))
logging.info(log_string_info.format(var_name, len(var_value), type(var_value)))
if log_level_set == logging.DEBUG:
#logging.debug(""+log_string_debug,var_name, len(var_value), type(var_name),var_value)
logging.debug(log_string_debug.format(var_name, len(var_value), type(var_name), var_value))
What could be the reason for the error ?

PyInstaller fails to find fonts.json

I want to make a word cloud in Korean. My OS is Windows. I want to make the Python code into an exe file that can run at any desktop. I'm trying to use PyInstaller to make the exe file.
pyinstaller.exe --onefile --icon=Pikachu.ico wordcloud.py
But I get this error:
Traceback (most recent call last):
File "wordcloud.py", line 6, in <module>
File "C:\Users\Myungho Lee\Downloads\pyinstaller-develop\pyinstaller-develop\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
File "site-packages\pytagcloud\__init__.py", line 26, in <module>
IOError: [Errno 2] No such file or directory: 'C:\\Users\\Myungho Lee\\AppData\\Local\\Temp\\_MEI67722\\pytagcloud\\fonts\\fonts.json'
Failed to execute script wordcloud
This is my code: (wordcloud.py)
#-*- coding: utf-8 -*-
import fnmatch
import os
import random
from PyInstaller.utils.hooks import collect_data_files
import pytagcloud
import simplejson
from pytagcloud import LAYOUT_HORIZONTAL
# requires Korean font support
import csv
import pygame
def draw_cloud(tags, filename, fontname, size):
pytagcloud.create_tag_image(tags, filename, fontname = fontname, size = size, rectangular=True, layout=LAYOUT_HORIZONTAL)
r = lambda: random.randint(0,255)
color = lambda: (r(), r(), r())
fonts = pygame.font.get_fonts()
#datas = collect_data_files('fonts')
current_dir = os.path.dirname(os.path.abspath(__file__))
current_dir = current_dir.replace('\\', '/')
csv_dir = current_dir+"/csv/"
font_path = current_dir + "/fonts/"
print font_path
FONT_CACHE = simplejson.load(open(os.path.join(font_path, 'fonts.json'), 'r'))
print FONT_CACHE
path = csv_dir
file_list = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.csv')]
for i in range(0, len(file_list)):
file_list[i] = file_list[i].replace('\\', '/')
file_list[i] = file_list[i].decode('cp949').encode('utf-8')
file_list[i] = unicode(file_list[i], 'utf-8')
tmp_str = file_list[i].split("/")
file_len = len(tmp_str)
for fileName in file_list:
with open(fileName, 'rb') as csvfile:
dic = []
count = False
reader = csv.reader(csvfile, delimiter = ',', quotechar = '|')
for row in reader:
try:
if(int(row[2]) > 500):
freq = int(int(row[2]) * 0.4)
count = True
elif(int(row[2]) > 400):
freq = int(int(row[2])*0.5)
count = True
elif(int(row[2])> 300):
freq = int(int(row[2]) * 0.6)
count = False
elif(int(row[2]) > 200):
freq = int(int(row[2])* 0.65)
count = False
elif(int(row[2]) > 100):
freq = int(int(row[2]) * 0.7)
count = False
else:
freq = int(int(row[2])* 0.75)
count = False
if(count):
sizediv = 3
else:
sizediv = 2
dic.append({'color': color(), 'tag': unicode(row[1], 'cp949'), 'size': freq/sizediv})
except:
continue
tags = dic
if (dic[0]['size'] > 70):
size = (600, 600)
elif (dic[0]['size'] > 60):
size = (550, 550)
elif (dic[0]['size'] > 50):
size = (450, 450)
elif (dic[0]['size'] > 40):
size = (400, 400)
elif (dic[0]['size'] > 30):
size = (350, 350)
cloudName = fileName.split('/csv/')[1]
cloudName = cloudName.split('.csv')[0]
dirName = fileName.split('/csv/')[0]
draw_cloud(tags, dirName + '/word_cloud/'+cloudName+ '.png', FONT_CACHE[0]['name'], size)

Categories