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 upload component #622

Merged
merged 13 commits into from
Mar 4, 2023
Merged

Add upload component #622

merged 13 commits into from
Mar 4, 2023

Conversation

picklelo
Copy link
Contributor

@picklelo picklelo commented Mar 2, 2023

Example app showcasing new component:

import pynecone as pc

class State(pc.State):
    """The app state."""

    # The image to show.
    img: str

    async def handle_upload(self, file: pc.UploadFile):
        """Handle the upload of a file.
        
        Args:
            file: The uploaded file.
        """
        upload_data = await file.read()
        outfile = f".web/public/{file.filename}"

        # Save the file.
        with open(outfile, "wb") as file_object:
            file_object.write(upload_data)

        # Update the img var.
        self.img = file.filename


def index():
    """The main view."""
    return pc.vstack(
        pc.upload(
            pc.button("Select File"),
            pc.text("Drag and drop files here or click to select files"),
            border="1px dotted black",
            padding="20em",
        ),
        pc.button(
            "Upload", 
            on_click=lambda: State.handle_upload(pc.upload_files())
        ),
        pc.image(src=State.img),
    )


# Add state and page to the app.
app = pc.App(state=State)
app.add_page(index, title="Upload")
app.compile()
Screen.Recording.2023-03-03.at.4.34.45.PM.mov

@picklelo picklelo mentioned this pull request Mar 3, 2023
@Alek99 Alek99 self-requested a review March 4, 2023 02:19
@picklelo picklelo linked an issue Mar 4, 2023 that may be closed by this pull request
@picklelo picklelo merged commit f7138bd into main Mar 4, 2023
@picklelo picklelo deleted the nikhil/upload branch March 4, 2023 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add File Upload Component (pc.upload)
2 participants