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 sheet_index
param in datafile API to handle excel files with multiple sheets
#3195
Conversation
@dmos62 PTAL. Thanks! |
mathesar/imports/excel.py
Outdated
try: | ||
dataframe = remove_empty_rows_and_columns_from_dataframe( | ||
pandas.read_excel(data_file.file.path, data_file.sheet_name, header=header_row) | ||
) | ||
except ValueError: | ||
# If sheet number has been passed as a parameter instead of sheet name. | ||
dataframe = remove_empty_rows_and_columns_from_dataframe( | ||
pandas.read_excel(data_file.file.path, int(data_file.sheet_name), header=header_row) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit skeptical about this, I think we should only use the index of the file so that our django model could stay consistent, this could be achieved by searching the name of the excel sheet in the list of all the sheet_names and then finding its index. However, finding the list of sheet_names could cause a lot of overhead using the ExcelFile.sheet_names
or df.keys()
as they require to load the whole excel file in memory instead of just the specified sheet as done here. I did however find a method to efficiently extract sheet names but idk if its worth it. I'd like to know your thoughts on this @dmos62.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice there's some confusion between index and name here. Sometimes the sheet_name
is cast to an int
, which I presume implies that it acts as an index. sheet_index
and sheet_name
should be distinct and if we'd want to use some kind of polymorphic variable that's sometimes a name and sometimes an index, we'd probably name it sheet_id
(since both a name and an index are identifiers), but I don't like the idea of a variable sometimes being one thing and sometimes something else.
For what it's worth, I think an index identifier has a better simplicity/usefulness relation than a name identifier. We could support both, but keeping it minimal is desirable right now, I think.
The SO answer @Anish9901 linked to mentioned that loading excel files gets exponentially slower with size (in Pandas). Supposing that's true, we'd do well to fix that at some point. Maybe now. If not, we should definitely put a TODO
comment with a link to that SO thread explaining what the problem is and what the solution could be (where the relevant code is found).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth, I think an index identifier has a better simplicity/usefulness relation than a name identifier. We could support both, but keeping it minimal is desirable right now, I think.
Makes sense. I'll update the attribute to sheet_index
instead of having a polymorphic variable.
mathesar/tests/api/test_table_api.py
Outdated
@@ -242,6 +242,7 @@ def _create_table(client, data_files, table_name, schema, import_target_table, d | |||
|
|||
response = client.post('/api/db/v0/tables/', body) | |||
response_table = response.json() | |||
print('\n\n', response_table, '\n\n') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You missed a print statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Anish9901, removed it now
@dmos62 @Anish9901 I have updated the PR. PTAL. Thanks! |
sheet_name
param in datafile API to handle excel files with multiple sheetssheet_index
param in datafile API to handle excel files with multiple sheets
mathesar/utils/datafiles.py
Outdated
@@ -114,18 +114,17 @@ def create_datafile(data): | |||
) | |||
else: | |||
max_level = data.get('max_level', 0) | |||
sheet_name = data.get('sheet_name', '1') | |||
sheet_index = data.get('sheet_index', 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have default be 1 if the index is 0-indexed? This also conflicts with the analogous field in the DataFile
django model whose default is 0. Is this an oversight?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was reducing the sheet_index
value received from the API just a line below, so I thought that if the POST request doesn't have this value, we can assign it default value 1 and then reduce it.
On second thought, I agree that the above method is confusing. I have two solutions:
- Keep the value 0-indexed everywhere and inform users to give sheet index based on 0-indexing.
- Keep the value 1-indexed everywhere -- when asking users and when assigning default value (which would be 1). We'll just reduce it when we call
pandas.read_excel()
.
@dmos62 what are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think 0-indexing is standard and we should go with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmos62 I have updated it now. PTAL. Thanks!
e7b175b
Fixes part of #3027
This PR does the following:
sheet_index
parameter which is the sheet number in an excel file.header
is made false (i.e, use default column names).Screenshots
sheet-name.mp4
Checklist
Update index.md
).develop
branch of the repositoryvisible errors.
Developer Certificate of Origin
Developer Certificate of Origin