Skip to content

Commit

Permalink
Merge pull request #75 from CambioML/jojo-branch
Browse files Browse the repository at this point in the history
Add file ovewrite handling for CSV save
  • Loading branch information
goldmermaid authored Oct 13, 2023
2 parents 1c4e111 + 2c263a4 commit 552eaa2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
12 changes: 12 additions & 0 deletions pykoi/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,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-fb0d7a3f.js"></script>
<link rel="stylesheet" href="/assets/index-10f2d9f3.css">
</head>
<body>
Expand Down
31 changes: 26 additions & 5 deletions pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script>
import { onDestroy, onMount } from "svelte";
import Modal from "../../UIComponents/Modal.svelte";
export let showModal, table;
let dialog;
Expand All @@ -8,11 +7,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 +33,19 @@
}
};
const handleSubmit = async (e) => {
e.preventDefault();
const response = await fetch(
`/file_exists/?file_name=${file_name}.csv`
);
const { file_exists } = await response.json();
if ( 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 +74,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 +83,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

0 comments on commit 552eaa2

Please sign in to comment.