Issue
There was a problem with saving the file to the specified path. Here is a part of the code that is not executed until the end (i.e. it receives a response from the server, and then silence, although a dataframe should be formed for subsequent saving to a file):
# accepting the server response
def recv_msg(self):
data = self.sock.recv(10240)
data = pickle.loads(data, encoding='utf-8')
rep_file = data.to_csv('report_df.csv', index=False)
target_fld = self.get_folder()
os.path.join(str(target_fld), rep_file)
print('file upload')
self.sock.close()
Here is all the code in the module:
import sys
import os
import socket
import pickle
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QApplication
from upload_client_gui import Ui_MainWindow
class gui(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(('localhost', 9090))
self.download_folder = None
self.ui.pushButton.clicked.connect(self.get_folder)
self.ui.pushButton_2.clicked.connect(self.send_msg)
# selecting the date of the report to be uploaded
def calendar_date(self):
date = self.ui.calendarWidget.selectedDate()
string_date = str(date.toPyDate())
return string_date
# the file explorer opens to select the folder where the file will be saved
def get_folder(self):
try:
self.download_folder = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select a folder to save')
os.chdir(self.download_folder)
print(self.download_folder)
except:
pass
# sending a date to the server via a socket
def send_msg(self):
msg = self.calendar_date()
print(msg)
self.sock.send(msg.encode('utf-8'))
# accepting the server response
def recv_msg(self):
data = self.sock.recv(10240)
data = pickle.loads(data, encoding='utf-8')
rep_file = data.to_csv('report_df.csv', index=False)
target_fld = self.get_folder()
os.path.join(str(target_fld), rep_file)
print('file upload')
self.sock.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = gui()
win.show()
sys.exit(app.exec())
The folder selection works, the absolute path is returned, the socket request and response are also there.
Please help me figure out where my problem is.
In the version without GUI, the socket works properly.
Solution
I figured out my mistakes! It was necessary to put sending and receiving messages in one method, and slightly rewrite the creation of a file in the selected folder. I hope it will be useful to someone:
import sys
import os
import socket
import pickle
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QApplication
from upload_client_gui import Ui_MainWindow
class Gui(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(('localhost', 9090))
self.download_folder = None
self.ui.pushButton.clicked.connect(self.get_folder)
self.ui.pushButton_2.clicked.connect(self.send_msg)
# selecting the date of the report to be uploaded
def calendar_date(self):
date = self.ui.calendarWidget.selectedDate()
string_date = str(date.toPyDate())
return string_date
# the file explorer opens to select the folder where the file will be saved
def get_folder(self):
try:
self.download_folder = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select a folder to save')
self.download_folder = self.download_folder.replace('/', '\\') + '\\'
except:
pass
# sending the date to the server and receiving information from the server via a socket
def send_msg(self):
msg = self.calendar_date()
self.sock.send(msg.encode('utf-8'))
data = self.sock.recv(10240)
data = pickle.loads(data, encoding='utf-8')
try:
target_fld = self.download_folder
data.to_csv(os.path.join(str(target_fld), 'report_df.csv'), index=False)
self.sock.close()
except Exception as e:
print(e)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Gui()
win.show()
sys.exit(app.exec())
Answered By - Pavel_G
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.