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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion mathesar/api/serializers/data_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Meta:
model = DataFile
fields = [
'id', 'file', 'table_imported_to', 'user', 'header', 'delimiter',
'escapechar', 'quotechar', 'paste', 'url', 'created_from', 'max_level'
'escapechar', 'quotechar', 'paste', 'url', 'created_from', 'max_level', 'sheet_index'
]
extra_kwargs = {
'file': {'required': False},
Expand Down
3 changes: 2 additions & 1 deletion mathesar/imports/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def remove_empty_rows_and_columns_from_dataframe(df):
def create_db_table_from_excel_data_file(data_file, name, schema, comment=None):
db_name = schema.database.name
engine = create_mathesar_engine(db_name)
header_row = 0 if data_file.header else None
dataframe = remove_empty_rows_and_columns_from_dataframe(
pandas.read_excel(data_file.file.path)
pandas.read_excel(data_file.file.path, data_file.sheet_index, header=header_row)
)
column_names = process_column_names(dataframe.columns)
try:
Expand Down
18 changes: 18 additions & 0 deletions mathesar/migrations/0005_datafile_sheet_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.14 on 2023-09-01 04:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mathesar', '0004_shares'),
]

operations = [
migrations.AddField(
model_name='datafile',
name='sheet_name',
field=models.CharField(default='0', max_length=100),
),
]
22 changes: 22 additions & 0 deletions mathesar/migrations/0006_auto_20230906_0413.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.1.14 on 2023-09-06 04:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mathesar', '0005_datafile_sheet_name'),
]

operations = [
migrations.RemoveField(
model_name='datafile',
name='sheet_name',
),
migrations.AddField(
model_name='datafile',
name='sheet_index',
field=models.IntegerField(default=0),
),
]
1 change: 1 addition & 0 deletions mathesar/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ class DataFile(BaseModel):
base_name = models.CharField(max_length=100)
header = models.BooleanField(default=True)
max_level = models.IntegerField(default=0, blank=True)
sheet_index = models.IntegerField(default=0)
delimiter = models.CharField(max_length=1, default=',', blank=True)
escapechar = models.CharField(max_length=1, blank=True)
quotechar = models.CharField(max_length=1, default='"', blank=True)
Expand Down
32 changes: 32 additions & 0 deletions mathesar/tests/api/test_table_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,3 +2092,35 @@ def test_create_table_using_null_id_excel_data_file(client, null_id_excel_data_f
client, table_name, expt_name, null_id_excel_data_file, schema, first_row,
column_names, import_target_table=None
)


def _create_excel_datafile_using_sheet_index_param(filepath, sheet_index):
with open(filepath, "rb") as file:
data_file = DataFile.objects.create(
file=File(file),
created_from='file',
base_name='multiple_sheets',
type='excel',
sheet_index=sheet_index
)
return data_file


def test_create_table_with_multiple_sheets_excel_file(client, multiple_sheets_excel_filepath, schema):
column_names = ['Name', 'Age', 'Email']
test_datafile_objects_with_sheet_index = [
_create_excel_datafile_using_sheet_index_param(multiple_sheets_excel_filepath, sheet_index)
for sheet_index in range(3)
]
expected_first_row_data = [
(1, 'Jim', '25', 'jim@example.com'),
(1, 'John', '25', 'john@example.com'),
(1, 'Jake', '25', 'jake@example.com'),
]

for index, datafile in enumerate(test_datafile_objects_with_sheet_index):
table_name = f'Table {index}'
check_create_table_response(
client, table_name, table_name, datafile, schema, expected_first_row_data[index],
column_names, import_target_table=None
)
5 changes: 5 additions & 0 deletions mathesar/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ def null_id_table_excel_filepath():
return 'mathesar/tests/data/excel_parsing/null_id_table.xlsx'


@pytest.fixture(scope='session')
def multiple_sheets_excel_filepath():
return 'mathesar/tests/data/excel_parsing/multiple_sheets.xlsx'


@pytest.fixture
def db_table_to_dj_table(engine, create_schema):
"""
Expand Down
Binary file not shown.
6 changes: 5 additions & 1 deletion mathesar/utils/datafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ def create_datafile(data):
)
else:
max_level = data.get('max_level', 0)
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!

# Pandas indexes sheets from 0 and not 1.
sheet_index = sheet_index - 1
datafile = DataFile(
file=raw_file,
base_name=base_name,
type=type,
created_from=created_from,
header=header,
max_level=max_level
max_level=max_level,
dmos62 marked this conversation as resolved.
Show resolved Hide resolved
sheet_index=sheet_index
)
datafile.save()
raw_file.close()
Expand Down