Skip to content

Commit 957c0d8

Browse files
authored
Merge pull request #1264 from mito-ds/code-params-bug
Fix code params bug
2 parents 5c3cce1 + 0b0adce commit 957c0d8

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

mitosheet/src/mito/components/taskpanes/CodeOptions/CodeOptionsParameters.tsx

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from "react";
22
import { MitoAPI } from "../../../api/api";
3-
import { CodeOptions, ParamName, ParamSubType, ParamType, ParamValue, ParameterizableParams } from "../../../types";
3+
import { CodeOptions, ParamName, ParamSubType, ParamValue, ParameterizableParams } from "../../../types";
44

55
import { useStateFromAPIAsync } from "../../../hooks/useStateFromAPIAsync";
66
import DropdownButton from "../../elements/DropdownButton";
77
import DropdownItem from "../../elements/DropdownItem";
88
import Input from "../../elements/Input";
99
import LabelAndTooltip from "../../elements/LabelAndTooltip";
10+
import XIcon from "../../icons/XIcon";
1011
import Col from "../../layout/Col";
1112
import Row from "../../layout/Row";
12-
import XIcon from "../../icons/XIcon";
1313

1414

1515
interface CodeOptionsParametersProps {
@@ -18,8 +18,14 @@ interface CodeOptionsParametersProps {
1818
setCodeOptions: React.Dispatch<React.SetStateAction<CodeOptions>>;
1919
}
2020

21-
const getParamDisplayString = (paramValue: string, paramType: ParamType): string => {
22-
if (paramType === 'file_name') {
21+
/**
22+
* @param paramValue The value of the parameter
23+
* @param isFile - Whether the param is a file or not. This is calculated in different ways
24+
* depending on where we call it.
25+
* @returns A string to display the parameter value
26+
*/
27+
const getParamDisplayString = (paramValue: string, isFile: boolean): string => {
28+
if (isFile) {
2329
return getFileNameFromParamValue(paramValue);
2430
} else {
2531
return paramValue;
@@ -42,8 +48,6 @@ const getParamDescriptionString = (paramSubtype: ParamSubType): string => {
4248
} else {
4349
return paramSubtype;
4450
}
45-
46-
4751
}
4852

4953
const getFileNameFromParamValue = (paramValue: string): string => {
@@ -58,14 +62,14 @@ const getFileNameFromParamValue = (paramValue: string): string => {
5862
return fileName;
5963
}
6064

61-
const getDefaultParamName = (paramValue: string, paramType: ParamType): string => {
62-
if (paramType === 'file_name') {
65+
const getDefaultParamName = (paramValue: string, paramSubType: ParamSubType): string => {
66+
if (paramSubType === 'import_dataframe') {
67+
return paramValue;
68+
} else {
6369
const fileName = getFileNameFromParamValue(paramValue);
6470
const noExt = fileName.substring(0, fileName.indexOf('.')); // Remove the file extension
6571
const withUnderscores = noExt.replace(/[^a-zA-Z0-9]/g, '_'); // Replace all non-alphanumeric characters with underscores
6672
return withUnderscores + '_path';
67-
} else {
68-
return paramValue;
6973
}
7074
}
7175

@@ -115,13 +119,15 @@ const CodeOptionsParameters = (props: CodeOptionsParametersProps): JSX.Element =
115119
disabled={disabled}
116120
title={!props.codeOptions.as_function ? 'Toggle Generate Function before adding parameters.' : (parameterizableParams.length === 0 ? 'There are no available options to parameterize. Import data first.' : undefined)}
117121
>
118-
{unparametizedParams.map(([paramValue, paramType, paramSubtype], index) => {
122+
{unparametizedParams.map((paramInfo, index) => {
123+
const paramValue = paramInfo[0];
124+
const paramSubtype = paramInfo[2];
119125
const paramDescription = getParamDescriptionString(paramSubtype);
120126

121127
return (
122128
<DropdownItem
123129
key={index}
124-
title={getParamDisplayString(paramValue, paramType)}
130+
title={getParamDisplayString(paramValue, paramSubtype !== 'import_dataframe')}
125131
subtext={paramDescription}
126132
onClick={() => {
127133
props.setCodeOptions((prevCodeOptions) => {
@@ -130,7 +136,7 @@ const CodeOptionsParameters = (props: CodeOptionsParametersProps): JSX.Element =
130136
return prevCodeOptions;
131137
}
132138

133-
const paramName = getDefaultParamName(paramValue, paramType);
139+
const paramName = getDefaultParamName(paramValue, paramSubtype);
134140

135141
newCodeOptions.function_params[paramName] = paramValue;
136142
return newCodeOptions;
@@ -169,7 +175,7 @@ const CodeOptionsParameters = (props: CodeOptionsParametersProps): JSX.Element =
169175
<Row key={index} justify='space-between' align='center'>
170176
<Col span={8} offsetRight={2}>
171177
<p title={paramValue}>
172-
{getParamDisplayString(paramValue, paramValue.startsWith('r"') || paramValue.startsWith("r'") || paramValue.startsWith("'") ? 'file_name' : 'df_name')}
178+
{getParamDisplayString(paramValue, paramValue.startsWith('r"') || paramValue.startsWith("r'") || paramValue.startsWith("'"))}
173179
</p>
174180
</Col>
175181
<Col span={10} offsetRight={2}>

mitosheet/src/mito/types.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ export enum UpdateType {
7878

7979
export type ParamName = string;
8080
export type ParamValue = string;
81-
export type ParamType = 'file_name' | 'df_name'
81+
export type ParamType = 'import' | 'export'
8282
export type ParamSubType = 'import_dataframe'
8383
| 'file_name_export_excel'
8484
| 'file_name_export_csv'
8585
| 'file_name_import_excel'
8686
| 'file_name_import_csv'
87+
| 'all' // This represents all of the above
8788
export type ParameterizableParams = [ParamValue, ParamType, ParamSubType][];
8889

8990
export type CodeOptions = {

tests/streamlit_ui_tests/taskpanes/code_config.spec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from '@playwright/test';
2-
import { checkOpenTaskpane, clickTab, getMitoFrameWithTestCSV } from '../utils';
2+
import { awaitResponse, checkOpenTaskpane, clickTab, getMitoFrameWithTestCSV } from '../utils';
33

44

55
test.describe('Code Config', () => {
@@ -18,16 +18,28 @@ test.describe('Code Config', () => {
1818

1919
test('Configure Code to generate function with parameters', async ({ page }) => {
2020
const mito = await getMitoFrameWithTestCSV(page);
21+
await mito.locator('.mito-toolbar-button', { hasText: 'Export'}).click();
22+
await mito.locator('.mito-dropdown-item', { hasText: 'Download File when Executing Code'}).click();
23+
await awaitResponse(page);
24+
await mito.getByText('Generate Export Code').click();
25+
2126
await clickTab(page, mito, 'Code');
2227

2328
await mito.getByRole('button', { name: 'Configure Code' }).click();
2429
await checkOpenTaskpane(mito, 'Generated Code Options');
2530

2631
await mito.locator('.spacing-row', { hasText: 'Generate Function' }).locator('.toggle').click();
2732
await mito.getByRole('textbox').fill('new name');
33+
await awaitResponse(page);
2834
await mito.getByText('Add').click();
2935
await mito.locator('.mito-dropdown-item', { hasText: 'CSV Import File Path' }).click();
36+
await awaitResponse(page);
3037

31-
await expect(page.locator('.stCodeBlock')).toContainText('def new_name(r\'');
38+
await mito.getByText('Add').click();
39+
await mito.locator('.mito-dropdown-item', { hasText: 'CSV Export File Path' }).click();
40+
41+
await expect(page.locator('.stCodeBlock')).toContainText('def new_name(test_path, test_export_path):');
42+
await expect(page.locator('.stCodeBlock')).toContainText(/test_path = r'[:a-zA-Z0-9\/\\]*test\.csv'/);
43+
await expect(page.locator('.stCodeBlock')).toContainText(/test_export_path = r'test_export.csv'/);
3244
});
3345
});

0 commit comments

Comments
 (0)