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 sheet_index param in datafile API to handle excel files with multiple sheets #3195

Merged
merged 6 commits into from Sep 8, 2023

Conversation

IamEzio
Copy link
Contributor

@IamEzio IamEzio commented Sep 1, 2023

Fixes part of #3027

This PR does the following:

  • Adds code to accept sheet_index parameter which is the sheet number in an excel file.
  • Updates excel importing code to handle cases when header is made false (i.e, use default column names).

Screenshots

sheet-name.mp4

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@IamEzio IamEzio marked this pull request as draft September 1, 2023 05:43
@IamEzio IamEzio marked this pull request as ready for review September 1, 2023 06:29
@IamEzio IamEzio requested a review from dmos62 September 1, 2023 06:29
@IamEzio
Copy link
Contributor Author

IamEzio commented Sep 1, 2023

@dmos62 PTAL. Thanks!

@IamEzio IamEzio added this to the GSoC 2023 milestone Sep 1, 2023
@IamEzio IamEzio added the work: backend Related to Python, Django, and simple SQL label Sep 1, 2023
Comment on lines 54 to 62
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)
)
Copy link
Member

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.

Copy link
Contributor

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).

Copy link
Contributor Author

@IamEzio IamEzio Sep 4, 2023

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.

@@ -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')
Copy link
Member

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.

Copy link
Contributor Author

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

mathesar/utils/datafiles.py Show resolved Hide resolved
@IamEzio
Copy link
Contributor Author

IamEzio commented Sep 6, 2023

@dmos62 @Anish9901 I have updated the PR. PTAL. Thanks!

@IamEzio IamEzio changed the title Add sheet_name param in datafile API to handle excel files with multiple sheets Add sheet_index param in datafile API to handle excel files with multiple sheets Sep 6, 2023
@@ -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)
Copy link
Contributor

@dmos62 dmos62 Sep 6, 2023

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?

Copy link
Contributor Author

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:

  1. Keep the value 0-indexed everywhere and inform users to give sheet index based on 0-indexing.
  2. 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?

Copy link
Contributor

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.

Copy link
Contributor Author

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!

@dmos62 dmos62 added this pull request to the merge queue Sep 8, 2023
Merged via the queue into mathesar-foundation:develop with commit e7b175b Sep 8, 2023
9 checks passed
@IamEzio IamEzio deleted the sheet-number branch September 11, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
work: backend Related to Python, Django, and simple SQL
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants