How to save data from pandastable? - python

I'm creating a table interface in which the user will receive a table from pandastable and write some data. Then, I need to save the updated pandastable to make some evaluations. How could I do this?
This is the code:
from tkinter import *
from pandastable import Table, TableModel
import pandas as pd
class TestApp(Frame):
def __init__(self, parent=None):
self.parent = parent
Frame.__init__(self)
self.main = self.master
self.main.geometry('600x400+200+100')
self.main.title('Table app')
f = Frame(self.main)
f.pack(fill=BOTH,expand=1)
df = pd.read_excel('2018.xlsx','November',7)
x = 'desc'
nome = df[x].values.tolist()
name = []
for i in range(len(nome)):
if nome[i] == nome[i] and nome[i] != x:
name.append(nome[i])
df1 = pd.DataFrame({x:name})
cfop = [float('Nan') for i in range(len(name))]
cst_in = [float('Nan') for i in range(len(name))]
cst_out = [float('Nan') for i in range(len(name))]
ncm = [float('Nan') for i in range(len(name))]
icms = [float('Nan') for i in range(len(name))]
df1['ncm'] = pd.Series(ncm)
df1['CST in'] = pd.Series(cst_in)
df1['CST out'] = pd.Series(cst_out)
df1['CFOP'] = pd.Series(cfop)
df1['ICMS'] = pd.Series(icms)
self.table = pt = Table(f, dataframe=df1, showtoolbar=True, showstatusbar=True)
pt.show()
return
app = TestApp()
app.mainloop()
df1 = Table.model.df1
The user is expected to fill the columns ncm, CFOP, ICMS, CST in and CST out. Right now, he can write on those columns, but the data is lost once he closes the app. I want to get the data that he writes and put in a variable DataFrame.

You can use the following line:
pt.doExport(filename="test2.csv")
This will result in a .csv file with all of the data from the table.

Related

Update list in combo box based on another combo box selection

I made two combo boxes (kategoriCombo and unsurCombo). I want to populate the unsurCombo using a list that i get from parsing api based on the kategoriCombo selection. I saw this post and try it https://stackoverflow.com/questions/63224359/change-combobox-item-when-another-combobox-item-is-changed-in-pyqt5 but still doesn't work.
My code doesn't get any error but when I change the kategoriCombo, the unsurCombo doesn't update automatically even when i already use currentTextChanged and connect it to a function.
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'kugi_dialog_base.ui'))
urlKategori = "https://kugi.ina-sdi.or.id:8080/kugiapi/featurecatalog"
response = request.urlopen(urlKategori)
dataKategori = json.loads(response.read())
daftarKategori = []
for kategoriList in dataKategori:
namaKategori = kategoriList.get('name')
trimmedKategori = namaKategori.strip('#en')
daftarKategori.append(trimmedKategori)
class kugiDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(kugiDialog, self).__init__(parent)
self.setupUi(self)
self.kategoriCombo.addItems(daftarKategori)
self.kategoriCombo.currentTextChanged.connect(self.changeKategori)
self.outputButton.clicked.connect(self.outFolder)
def changeKategori(self):
daftarID =[]
for dataID in dataKategori:
id = dataID.get('id')
idKategori = id.strip('#en')
daftarID.append(idKategori)
zippedKategori = zip(daftarID, daftarKategori)
selectedCategory = self.kategoriCombo.currentText()
idDipilih = []
for a, b in zippedKategori:
if b == selectedCategory:
idDipilih.append(a)
inputID = (idDipilih[0])
url = "https://kugi.ina-sdi.or.id:8080/kugiapi/featuretype?fcid="
response = request.urlopen(url+inputID)
data = json.loads(response.read())
daftarUnsur= []
for listdata in data:
unsur = listdata.get('typeName')
namaUnsur = unsur.strip('#en')
daftarUnsur.append(namaUnsur)
self.unsurCombo.addItems(daftarUnsur)

Python, Streamlit AgGrid add new row to AgGrid Table

I am trying to add a new row to an AgGrid Table using streamlit and python
At this point, I just want to add 1 or more new rows to the table generated by the AgGrid by pressing the "add row" button.
After pressing the "add row" button I generate a second table with the new row mistakenly, so I get 2 data-tables instead of updating the main table.
The initial data df = get_data() is been gathered from a SQL query. I want to add a new row and (for now) save it into a CSV file or at least get the updated DF with the new row added as an output and graph it
My current code
import streamlit as st
from metrics.get_metrics import get_data
from metrics.config import PATH_SAMPLES
filename: str = 'updated_sample.csv'
save_path = PATH_SAMPLES.joinpath(filename)
def generate_agrid(data: pd.DataFrame):
gb = GridOptionsBuilder.from_dataframe(data)
gb.configure_default_column(editable=True) # Make columns editable
gb.configure_pagination(paginationAutoPageSize=True) # Add pagination
gb.configure_side_bar() # Add a sidebar
gb.configure_selection('multiple', use_checkbox=True,
groupSelectsChildren="Group checkbox select children") # Enable multi-row selection
gridOptions = gb.build()
grid_response = AgGrid(
data,
gridOptions=gridOptions,
data_return_mode=DataReturnMode.AS_INPUT,
update_on='MANUAL', # <- Should it let me update before returning?
fit_columns_on_grid_load=False,
theme=AgGridTheme.STREAMLIT, # Add theme color to the table
enable_enterprise_modules=True,
height=350,
width='100%',
reload_data=True
)
data = grid_response['data']
selected = grid_response['selected_rows']
df = pd.DataFrame(selected) # Pass the selected rows to a new dataframe df
return grid_response
def onAddRow(grid_table):
df = pd.DataFrame(grid_table['data'])
column_fillers = {
column: (False if df.dtypes[column] == "BooleanDtype"
else 0 if df.dtypes[column] == "dtype('float64')"
else '' if df.dtypes[column] == "string[python]"
else datetime.datetime.utcnow() if df.dtypes[column] == "dtype('<M8[ns]')"
else '')
for column in df.columns
}
data = [column_fillers]
df_empty = pd.DataFrame(data, columns=df.columns)
df = pd.concat([df, df_empty], axis=0, ignore_index=True)
grid_table = generate_agrid(df)
return grid_table
# First data gather
df = get_data()
if __name__ == '__main__':
# Start graphing
grid_table = generate_agrid(df)
# add row
st.sidebar.button("Add row", on_click=onAddRow, args=[grid_table])
Here is a sample minimal code.
import streamlit as st
import pandas as pd
from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode
def generate_agrid(df):
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gridoptions = gb.build()
grid_response = AgGrid(
df,
height=200,
gridOptions=gridoptions,
update_mode=GridUpdateMode.MANUAL
)
selected = grid_response['selected_rows']
# Show the selected row.
if selected:
st.write('selected')
st.dataframe(selected)
return grid_response
def add_row(grid_table):
df = pd.DataFrame(grid_table['data'])
new_row = [['', 100]]
df_empty = pd.DataFrame(new_row, columns=df.columns)
df = pd.concat([df, df_empty], axis=0, ignore_index=True)
# Save new df to sample.csv.
df.to_csv('sample.csv', index=False)
def get_data():
"""Reads sample.csv and return a dataframe."""
return pd.read_csv('sample.csv')
if __name__ == '__main__':
df = get_data()
grid_response = generate_agrid(df)
st.sidebar.button("Add row", on_click=add_row, args=[grid_response])
Initial output
Output after pressing add row
sample.csv
team,points
Lakers,120
Celtics,130

how to append a data at a list and plot it with python plotly

hi there i m still trying to get trade bot and try to plot them with their time and low price data.
i wanna get buy signals that i ve specified at if condition (when macdh turns from negative to positive). then i want to plot them at a data. but can not add them at buy_signal=[] place.
my error is
self.plotData(buy_signals = buy_signals)
IndexError: list index out of range
import requests
import json
from stockstats import StockDataFrame as Sdf
import plotly.graph_objects as go
from plotly.offline import plot
class TradingModel:
def __init__(self, symbol):
self.symbol = symbol
self.df = self.getData
def getData(self):
# define URL
base = 'https://api.binance.com'
endpoint = '/api/v3/klines'
params = '?&symbol='+self.symbol+'&interval=4h'
url = base + endpoint + params
# download data
data = requests.get(url)
dictionary = data.json()
# put in dataframe and clean-up
df = pd.DataFrame.from_dict(dictionary)
df = df.drop(range(6, 12), axis=1)
# rename columns and stockstasts
col_names = ['time', 'open', 'high', 'low', 'close', 'volume']
df.columns = col_names
stock = Sdf.retype(df)
for col in col_names:
df[col]=df[col].astype(float)
#defined macdh
df['macdh']=stock['macdh']
return (df)
def strategy(self):
df = self.df
buy_signals=[]
for i in range(1, len(df['close'])):
if df['macdh'].iloc[-1]>0 and df['macdh'].iloc[-2]<0:
buy_signals.append([df['time'][i], df['low'][i]])
self.plotData(buy_signals = buy_signals)
def plotData(self,buy_signal=False):
df=self.df
candle=go.Candlestick(
x=df['time'],
open=df['open'],
close=df['close'],
high=df['high'],
low=df['low'],
name="Candlesticks"
)
macdh=go.Scatter(
x=df['time'],
y=df['macdh'],
name="Macdh",
line = dict(color=('rgba(102, 207, 255, 50)')))
Data=[candle,macdh]
if buy_signals:
buys = go.Scatter(
x = [item[0] for item in buy_signals],
y = [item[1] for item in buy_signals],
name = "Buy Signals",
mode = "markers",
)
sells = go.Scatter(
x = [item[0] for item in buy_signals],
y = [item[1]*1.04 for item in buy_signals],
name = "Sell Signals",
mode = "markers",
)
data = [candle, macdh, buys, sells]
# style and display
layout = go.Layout(title = self.symbol)
fig = go.Figure(data = data, layout = layout)
plot(fig, filename=self.symbol)
def Main():
symbol = "BTCUSDT"
model = TradingModel(symbol)
model.strategy()
if __name__ == '__main__':
Main() ```
You need to replace :
self.plotData(buy_signals[i]) by self.plotData(buy_signals)
def plotData(self,buy_signal=False): by def plotData(self,buy_signals=None):
And it should be good to go !

py to exe : failed to execute script pyi_rth_win32comgenpy

I'm creating a simple calculation program using tkinter module and want to convert to exe as I want it to be executable at any pc. But somehow the error message show (failed to execute script pyi_rth_win32comgenpy).
I've try used pyinstaller ( cmd and the one on GitHub at : https://github.com/brentvollebregt/auto-py-to-exe) but to no avail. I also try using both types of python file (.py and .pyw)
from tkinter import *
from tkinter.filedialog import askopenfilename
import pandas as pd
from tkinter import messagebox
from pandastable import Table, TableModel
class Window(Frame):
def __init__(self, master =None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title('GUI')
self.pack(fill=BOTH, expand=1)
quitButton = Button(self, text='quit', command=self.client_exit)
quitButton.place(x=0, y=230)
# fileButton = Button(self, text='Browse Data Set', command=self.import_data)
# fileButton.place(x=150, y=0)
fileButton = Button(self, text='SBO', command=self.sbo)
fileButton.place(x=200, y=50)
fileButton = Button(self, text='CBO', command=self.cbo)
fileButton.place(x=150, y=50)
# menu = Menu(self.master)
# self.master.config(menu=menu)
#
# file = Menu(menu)
# file.add_command(label='Save',command=self.client_exit)
# file.add_command(label='Exit', command= self.client_exit)
# menu.add_cascade(label='File', menu=file)
#
# edit = Menu(menu)
# edit.add_command(label='Undo')
# menu.add_cascade(label='Edit', menu=edit)
def client_exit(self):
exit()
# def import_data(self):
#
# csv_file_path = askopenfilename()
# # print(csv_file_path)
# df = pd.read_excel(csv_file_path)
# return df
def sbo(self):
csv_file_path = askopenfilename()
df = pd.read_excel(csv_file_path)
data = df.drop(df.index[0]) # remove first row
data['BOVal%'] = data['BOVal%'].astype(str) # convert to string
data['BOQty%'] = data['BOQty%'].astype(str)
data['CustomerPONo'] = data['CustomerPONo'].astype(str)
data['OrdNo'] = data['OrdNo'].astype(str)
data['VendorNo'] = data['VendorNo'].astype(str)
pivot = data.pivot_table(index='Style', aggfunc='sum') # first pivot
pivoted = pd.DataFrame(pivot.to_records()) # flattened
pivoted = pivoted.sort_values(by=['BOVal'], ascending=False) # sort largest to smallest
pivoted['Ranking'] = range(1, len(pivoted) + 1) # Ranking
cols = pivoted.columns.tolist()
cols = cols[-1:] + cols[:-1]
pivoted = pivoted[cols]
pivoted = pivoted.set_index('Ranking')
col = df.columns.tolist()
col = (col[22:23] + col[15:17] + col[:14] + col[17:22] + col[23:37]) # rearrange column
data = df[col]
data = data.sort_values(by=['BOVal'], ascending=False) # sort value
data['Ranking'] = range(1, len(data) + 1) # Set rank
colm = data.columns.tolist()
colm = colm[-1:] + colm[:-1] # rearrange rank column
data = data[colm]
data = data.set_index('Ranking')
# sumboval = data['BOVal'].sum()
# sumboqty = data['BOQty'].sum()
# rounded = sumboval.round()
dates = data['SnapShotDate']
# print(dates)
dates = dates.iloc[1].strftime('%d%m%Y')
sos = data['SOS']
sos = sos[2]
result = pivoted.iloc[:10, :3]
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('%s SBO %s .xlsx' % (sos, dates), engine='xlsxwriter')
# Write each dataframe to a different worksheet.
result.to_excel(writer, sheet_name='pivot')
df.to_excel(writer, sheet_name=dates)
data.to_excel(writer, sheet_name='SBO')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
messagebox.showinfo("Note", "Calculation Completed")
def cbo(self):
csv_file_path = askopenfilename()
Stylemat = askopenfilename()
df = pd.read_excel(csv_file_path)
sm = pd.read_excel(Stylemat)
df = df.drop(df.index[0])
df.insert(loc=8, column='PH', value=['' for i in range(df.shape[0])])
df.insert(loc=9, column='Site', value=['' for i in range(df.shape[0])])
df['Region'] = df['Region'].fillna('"NA"')
df['S&OP Style Aggrt'] = df['S&OP Style Aggrt'].astype(str)
sm['Style'] = sm['Style'].astype(str)
dates = df['Date_Rp']
# print(dates)
dates = dates.iloc[1]
w = list(dates)
w[1] = '-'
w[3] = '-'
temp = w[0]
w[0] = w[2]
w[2] = temp
dates = "".join(w)
rowcount = len(df)
rowstyle = len(sm)
i = 0
j = 0
Style = []
for i in range(rowcount):
for j in range(rowstyle):
if df.iloc[i, 7] == sm.iloc[j, 0]:
df.iloc[i, 8] = 'Horizon'
df.iloc[i, 9] = sm.iloc[j, 2]
table = pd.pivot_table(df[df.PH == 'Horizon'], index='S&OP Style Aggrt', columns='Region',
values='Net CBO Value', aggfunc='sum')
table['Grand Total'] = table.sum(axis=1)
table = table.sort_values(by=['Grand Total'], ascending=False)
table['Ranking'] = range(1, len(table) + 1)
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('CBO %s .xlsx' % dates, engine='xlsxwriter')
# Write each dataframe to a different worksheet.
table.to_excel(writer, sheet_name='pivot')
df.to_excel(writer, sheet_name=dates)
sm.to_excel(writer, sheet_name='StyleMat')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
messagebox.showinfo("Note", "Calculation Completed")
root = Tk()
root.geometry('400x300')
app = Window(root)
root.mainloop()
I'd like to know how to find the main reason for this error and where to look for it, is it either my scripting method is incorrect or is there any additional file or module that I need. Appreciate in advance for your help. Thank you
I uninstalled everything related to win32 (pypiwin32, pywin32, pywin32-ctypes, pywinpty) and then installed again and magically it worked.
Took the idea from here and here.
this is quite late, but the answer to that issue is just the py to exe cannot execute on numpy 1.17. after downgrade to numpy 1.16, the program can run normally.
You are getting this error failed to execute script pyi_rth_win32comgenpy as result of not including the images you used for you icons and labels
I included images of icon, Question mark and the title
copy this images and include it the directory you have your pyi_rth_win32comgenpy executable.

Selecting the Values in the Excel Rows Depending on the selection of Check Box in the GUI using Python Tkinter

I have a Excel as Below
|---------------------|------------------|
| Heading 1 | Heading 2 |
|---------------------|------------------|
| Row1 | Value 1 |
|---------------------|------------------|
| Row2 | Value 2 |
|---------------------|------------------|
I am reading from excel and Showing the Values of Heading 1 in the GUI
When I click on submit Button , I need to read the value/text of the CheckBox depending on the selection of the CheckBox and Create XML by using Excel for only selected values
Problem is How can I only select the values in the Excel , depending on the selection of check Box. (But I know how to identify which check box is checked ). But how to relate to Excel is I am facing problem
Note: I Know how to create XML from excel
I know how to identify when submit is clicked
GUI Code:
Config.Py
import tkinter as tk
import xlrd
import GetValueFromExcel
from GetValueFromExcel import ExcelValue
from array import array
from tkinter import *
from tkinter import ttk, Button
from tkinter import *
root = Tk()
class UICreation():
def __init__(self):
print ("I m in __init__")
self.tabControl = ttk.Notebook(root)
self.tab1 = ttk.Frame(self.tabControl)
self.tab2 = ttk.Frame(self.tabControl)
def tabcreation(self):
print ("I M in Tab Creation")
self.tabControl.add(self.tab1 , text="Tab1")
#self.tabControl(self.tab1, text= t)
##self.tabControl(self.tab1, )
self.tabControl.add(self.tab2, text="Tab2")
self.tabControl.grid()
def checkbox(self):
print ("I M in checkBox")
checkBox1 = Checkbutton(self.tab1, text=str(t[0]))
checkBox2 = Checkbutton(self.tab1, text=str(t[1]))
Checkbutton()
checkBox1.grid()
checkBox2.grid()
def button(self):
button = Button(self.tab1 , text="Submit", command=self.OnButtonClick)
button.grid()
def OnButtonClick(self):
print ("I am Working")
if __name__ == '__main__':
ui = UICreation()
ev = GetValueFromExcel.ExcelValue()
ev.readExcelValue()
t = ev.readExcelValue()
print(t)
ui.tabcreation()
ui.checkbox()
ui.button()
#ev = readExcelValue()
root.mainloop()
GetValueFromExcel.py
import xlrd
class ExcelValue():
def __init__(self):
self.wb=xlrd.open_workbook(r"C:\<FilePath>\Filename.xlsx")
#self.ws=self.wb.sheet_by_name("Sheet1")
for sheet in self.wb.sheets():
self.number_of_rows = sheet.nrows
self.number_of_columns = sheet.ncols
def readExcelValue(self):
result_data = []
row_data = []
for sheet in self.wb.sheets():
for curr_row in range(1, self.number_of_rows, 1):
#for curr_col in range(0, self.number_of_columns , 1):
#data = sheet.cell_value(curr_row, curr_col) # Read the data in the current cell
data = sheet.cell_value(curr_row, 0)
#print(data)
row_data.append(data)
result_data.append(row_data)
return result_data[1]
You can pass the value to Checkbutton like this:
def checkbox(self):
print ("I M in checkBox")
checkBox1 = Checkbutton(self.tab1, text=str(t.keys()[0]), variable=t.values()[0])
checkBox2 = Checkbutton(self.tab1, text=str(t.keys()[1]), variable=t.values()[1])
Checkbutton()
checkBox1.grid()
checkBox2.grid()
and you must change readExcelValue, like below:
def readExcelValue(self):
result_data = {}
for sheet in self.wb.sheets():
for curr_row in range(1, self.number_of_rows, 1):
data = sheet.cell_value(curr_row, 0)
value = sheet.cell_value(curr_row, 1)
result_data[data] = value
return result_data

Categories

Resources