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 file ovewrite handling for CSV save #75

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pykoi/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,18 @@ async def get_components(
]
)

@app.get("/file_exists/")
async def check_file_exists(
file_name: str,
user: Union[None, UserInDB] = Depends(self.get_auth_dependency()),
):
try:
file_path = f"{os.getcwd()}/{file_name}"
file_exists = os.path.exists(file_path)
return {"log": f"Check if {file_name} exists succeeded.", "file_exists": file_exists, "status": "200"}
except Exception as ex:
return {"log": f"Check if {file_name} exists failed: {ex}", "status": "500"}

def create_data_route(id: str, data_source: Any):
"""
Create data route for the application.
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pykoi/frontend/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte</title>
<script type="module" crossorigin src="/assets/index-dc2068fd.js"></script>
<script type="module" crossorigin src="/assets/index-f3b0b2c8.js"></script>
<link rel="stylesheet" href="/assets/index-10f2d9f3.css">
</head>
<body>
Expand Down
30 changes: 26 additions & 4 deletions pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
FILE_INPUT: 0,
DOWNLOADED: 1,
FAILED_DOWNLOAD: 2,
OVERWRITE: 3,
};
let downloadState = DOWNLOAD_STATE.FILE_INPUT;

const handleSubmit = async (e) => {
e.preventDefault();
const saveToCSV = async (file_name) => {
const request_body = {
file_name,
};
Expand All @@ -34,6 +34,19 @@
}
};

const handleSubmit = async (e) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!. May be worth destucturing the file_exists directly, since we're just using file_exists there
e.g.
const { file_exists } = await response.json();

then

    if (file_exists) {
        ...
    } else {
        ...
    }
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just updated with this!

e.preventDefault();
const response = await fetch(
`/file_exists/?file_name=${file_name}.csv`
);
const file_exists_response = await response.json();
if (file_exists_response.file_exists === true) {
downloadState = DOWNLOAD_STATE.OVERWRITE;
} else {
saveToCSV(file_name);
}
};

function handleClose() {
showModal = false;
downloadState = DOWNLOAD_STATE.FILE_INPUT;
Expand Down Expand Up @@ -62,7 +75,7 @@
{/if}
{#if downloadState === DOWNLOAD_STATE.DOWNLOADED}
<div>
✅ Data downloaded to ~/pykoi/{file_name}.csv
✅ Data downloaded to /pykoi/{file_name}.csv
</div>
<div class="btn-container">
<button on:click={handleClose}>Close</button>
Expand All @@ -71,10 +84,19 @@
{#if downloadState === DOWNLOAD_STATE.FAILED_DOWNLOAD}
<div>⚠️ Download failed. Please try again.</div>
<div class="btn-container">
<button on:click={() => downloadState = DOWNLOAD_STATE.FILE_INPUT}>Retry</button>
<button on:click={() => (downloadState = DOWNLOAD_STATE.FILE_INPUT)}>Retry</button>
<button on:click={handleClose}>Close</button>
</div>
{/if}
{#if downloadState === DOWNLOAD_STATE.OVERWRITE}
<div>
⚠️ {file_name}.csv already exists. Do you wish to overwrite it?
</div>
<div class="btn-container">
<button on:click={() => (downloadState = DOWNLOAD_STATE.FILE_INPUT)}>Back</button>
<button on:click={() => saveToCSV(file_name)}>Overwrite</button>
</div>
{/if}
</Modal>

<style>
Expand Down