Skip to content

Commit

Permalink
make the example nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
freemant2000 committed Aug 23, 2022
1 parent 00da577 commit 2704b13
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions examples/multiuploads.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from justpy import *

def on_submit(c, msg):
print(len(msg.form_data[0]["files"][0]["file_content"]))
print(len(msg.form_data[1]["files"][0]["file_content"]))
def handle_submit(c, msg):
fl=msg.page.file_list
fl.components.clear()
for fd in msg.form_data:
if "files" in fd:
for f in fd["files"]:
Li(text=f"File uploaded: {f['name']} of {len(f['file_content'])}", a=fl)

def multi_uploads():
wp=WebPage()
form=Form(a=wp, submit=on_submit)
Input(type="file", name="f1", a=form)
Input(type="file", name="f2", a=form)
Input(type="submit", value="OK", a=form)
return wp
def p1():
wp=WebPage()
f=Form(submit=handle_submit, a=wp)
Input(name="f1", type="file", a=f)
Input(name="f2", type="file", a=f)
Input(type="submit", value="OK", a=f)
wp.file_list=Ol(a=wp)
return wp


justpy(multi_uploads)
WebPage.tailwind=False
justpy(p1, start_server=(__name__=="__main__"))

2 comments on commit 2704b13

@sandeep-gh
Copy link

Choose a reason for hiding this comment

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

Can someone explain this example? It seems there is a form and submit button. Once the submit button is clicked, it will show list items with file names mentioned in the form.
I don't see this as an example for file upload. Also, why are naming the function p1. Call it launcher or endpoint or some such thing. p1 seems arbitary.

@freemant2000
Copy link
Contributor Author

@freemant2000 freemant2000 commented on 2704b13 Sep 1, 2022

Choose a reason for hiding this comment

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

File upload is done by the two <input type="file">. The actual upload process is done by Justpy when the <form> is submitted. The justpy website already has an example of file upload, but it didn't work when there were two or more file input fields. This example shows that the updated version works fine with multiple file input fields.

p1 means page 1. Yes, it is kind of arbitrary as it is not really relevant; only the file input fields are.

Please sign in to comment.