-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(file_upload): add new file upload feature in main.py and corre…
…sponding tests in spec.cy.ts This commit introduces a new feature that allows users to upload files. The main.py file contains the implementation of the file upload feature, while the spec.cy.ts file contains the tests for this feature.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import chainlit as cl | ||
|
||
|
||
@cl.on_file_upload(accept={"text/plain": [".py"]}) | ||
async def upload_file(files: any): | ||
for file_data in files: | ||
await cl.Message( | ||
content=f"`{file_data['name']}` uploaded, it contains {len(file_data['content'])} characters!" | ||
).send() | ||
|
||
|
||
@cl.on_chat_start | ||
async def start(): | ||
await cl.Message(content=f"Try to upload a file").send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { runTestServer } from "../../support/testUtils"; | ||
|
||
describe("Upload file", () => { | ||
before(() => { | ||
runTestServer(); | ||
}); | ||
|
||
it("should be able to upload and decode files", () => { | ||
cy.get("#upload-button").should("exist"); | ||
|
||
// Upload a text file | ||
cy.fixture("state_of_the_union.txt", "utf-8").as("txtFile"); | ||
cy.get("input[type=file]").selectFile("@txtFile", { force: true }); | ||
|
||
cy.get(".message") | ||
.eq(1) | ||
.should("contain", "state_of_the_union.txt uploaded, it contains"); | ||
|
||
// Expecting a python file, cpp file upload should be rejected | ||
cy.fixture("hello.cpp", "utf-8").as("cppFile"); | ||
cy.get("input[type=file]").selectFile("@cppFile", { force: true }); | ||
|
||
// Upload a python file | ||
cy.fixture("hello.py", "utf-8").as("pyFile"); | ||
cy.get("input[type=file]").selectFile("@pyFile", { force: true }); | ||
|
||
cy.get(".message") | ||
.eq(2) | ||
.should("contain", "hello.py uploaded, it contains"); | ||
}); | ||
}); |