Skip to content
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

add_row function breaks when using a numeric index #364

Open
info-rchitect opened this issue May 13, 2021 · 0 comments
Open

add_row function breaks when using a numeric index #364

info-rchitect opened this issue May 13, 2021 · 0 comments

Comments

@info-rchitect
Copy link

Environment

  • Operating System: WIndows 10
  • Python Version: 3.8.8
  • How did you install Qgrid: pip

Description of Issue

I am trying to add an empty row at the end of my qgrid widget. The row argument is:

[('ID', ''), ('SOURCE', ''), ('PROJECT', ''), ('NAME', ''), ('STATUS', ''), ('SHAPE', ''), ('TIME', '')]

The error is:

C:\notebook_analytics\nemawashi\nemawashi_lib\user_interfaces\jobs.py in add_jobs_to_qgrid_widget(self, job)
    340                 empty_row.append((column_name, ''))
    341             print(empty_row)
--> 342             self.widgets['jobs_qgrid'].add_row(empty_row)
    343         jobs_df = self.widgets['jobs_qgrid'].df
    344         (col_opts, col_defs) = self.__fetch_qgrid_options()

~\Anaconda3\envs\nemawashi\lib\site-packages\qgrid\grid.py in add_row(self, row)
   1617             added_index = self._duplicate_last_row()
   1618         else:
-> 1619             added_index = self._add_row(row)
   1620 
   1621         self._notify_listeners({

~\Anaconda3\envs\nemawashi\lib\site-packages\qgrid\grid.py in _add_row(self, row)
   1666         print(f"dictrow={dict(row)}")
   1667         print(f"indexname={df.index.name}")
-> 1668         index_col_val = dict(row)[df.index.name]
   1669 
   1670         # check that the given column names match what

KeyError: None

The issue here is that the documentation says the following for the _add_row function.

def _add_row(self, row):
        """
        Append a new row to the end of the DataFrame given a list of 2-tuples
        of (column name, column value). This method will work for DataFrames
        with arbitrary index types.
        """

However, the code actual assumes there is an index name set within the dataframe here. A dataframe with an integer index will not have an index name. Either the _add_row function needs to be re-written or an option could be added to the add_row function to add an empty row.

qgrid_widget.add_row(empty=True)

Adding an empty row is very easy regardless if there is an index or not.

Reproduction Steps

  1. Create a qgrid widget with an integer index
  2. Call the add_row function with a row argument

What steps have you taken to resolve this already?

The workaround for me is to add the empty row in pandas and then re-instantiate the qgrid widget.

Anything else?

Please advise on the PR direction folks want and I will create the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant