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

[Fix 71] Add example using pc.upload #72

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions upload/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pynecone.db
.web
__pycache__/
*.py[cod]
Binary file added upload/assets/favicon.ico
Binary file not shown.
10 changes: 10 additions & 0 deletions upload/pcconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pynecone as pc

class UploadConfig(pc.Config):
pass

config = UploadConfig(
app_name="upload",
db_url="sqlite:///pynecone.db",
env=pc.Env.DEV,
)
1 change: 1 addition & 0 deletions upload/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pynecone>=0.1.29
Empty file added upload/upload/__init__.py
Empty file.
80 changes: 80 additions & 0 deletions upload/upload/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import os
import asyncio
import pynecone as pc
from typing import List

class State(pc.State):
"""The app state."""
files_path:str = f".web/public/files/"
str_files:str=""
is_uploading:bool=False
async def handle_upload(self, files: List[pc.UploadFile]):
self.is_uploading = True
if(False is os.path.exists(self.files_path)):
os.makedirs(self.files_path, 0o755)
for file in files:
upload_data = await file.read()
output_filepath = self.files_path + file.filename
with open(output_filepath, "wb") as file_object:
file_object.write(upload_data)
return self.show_files

async def show_files(self):
await asyncio.sleep(1.0)
self.is_uploading = False
if(False is os.path.exists(self.files_path)):
os.makedirs(self.files_path, 0o755)
filelist = os.listdir(self.files_path)
self.str_files = ""
for filename in filelist:
self.str_files = self.str_files+filename+"\n"

color = "rgb(107,99,246)"
def index():
return pc.vstack(
pc.upload(
#pc.button("Select File", ),
pc.button(
"Select File(s)",
height="70px",
width="200px",
color=color,
bg="white",
border=f"1px solid {color}",
),
pc.text("Drag and drop files here or click to select files", height="100px", width="200px"),
border="1px dotted black",
padding="2em",
),
pc.hstack(
pc.button(
"Upload",
on_click=State.handle_upload(pc.upload_files()),
),
pc.button(
"Refresh",
on_click=State.show_files,
),
),
pc.heading("Files:"),
pc.cond(
State.is_uploading,
pc.progress(is_indeterminate=True, color="blue", width="100%"),
pc.progress(value=0, width="100%"),
),
pc.text_area(
is_disabled=True,
value=State.str_files,
width="100%",
height="100%",
bg="white",
color="black",
placeholder="No File",
min_height="20em",
),
)

# Add state and page to the app.
app = pc.App(state=State, on_load=State.show_files())
app.add_page(index, title="Upload")
app.compile()