Spaces:
Runtime error
Runtime error
Refactor drag-and-drop functionality in app.py to improve file input handling by introducing a helper function for input selection.
Browse files- __pycache__/test.cpython-310.pyc +0 -0
- app.py +7 -3
- test.py +83 -0
__pycache__/test.cpython-310.pyc
ADDED
|
Binary file (2.11 kB). View file
|
|
|
app.py
CHANGED
|
@@ -201,19 +201,23 @@ with gr.Blocks(css=css) as demo:
|
|
| 201 |
var root = document.getElementById("input-image");
|
| 202 |
if (!root || root.dataset.dropBound === "1") return;
|
| 203 |
|
| 204 |
-
var input = root.querySelector('input[type="file"]');
|
| 205 |
-
if (!input) return;
|
| 206 |
-
|
| 207 |
function prevent(e) {
|
| 208 |
e.preventDefault();
|
| 209 |
e.stopPropagation();
|
| 210 |
}
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
function onDrop(e) {
|
| 213 |
prevent(e);
|
| 214 |
var files = e.dataTransfer && e.dataTransfer.files;
|
| 215 |
if (!files || files.length === 0) return;
|
| 216 |
|
|
|
|
|
|
|
|
|
|
| 217 |
var dt = new DataTransfer();
|
| 218 |
dt.items.add(files[0]);
|
| 219 |
input.files = dt.files;
|
|
|
|
| 201 |
var root = document.getElementById("input-image");
|
| 202 |
if (!root || root.dataset.dropBound === "1") return;
|
| 203 |
|
|
|
|
|
|
|
|
|
|
| 204 |
function prevent(e) {
|
| 205 |
e.preventDefault();
|
| 206 |
e.stopPropagation();
|
| 207 |
}
|
| 208 |
|
| 209 |
+
function findInput() {
|
| 210 |
+
return root.querySelector('input[type="file"]') || root.querySelector("input");
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
function onDrop(e) {
|
| 214 |
prevent(e);
|
| 215 |
var files = e.dataTransfer && e.dataTransfer.files;
|
| 216 |
if (!files || files.length === 0) return;
|
| 217 |
|
| 218 |
+
var input = findInput();
|
| 219 |
+
if (!input) return;
|
| 220 |
+
|
| 221 |
var dt = new DataTransfer();
|
| 222 |
dt.items.add(files[0]);
|
| 223 |
input.files = dt.files;
|
test.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
css = """
|
| 4 |
+
#input-image {
|
| 5 |
+
min-height: 320px;
|
| 6 |
+
}
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
DROP_JS = """
|
| 10 |
+
<script>
|
| 11 |
+
(function () {
|
| 12 |
+
function bindDrop() {
|
| 13 |
+
var root = document.getElementById("input-image");
|
| 14 |
+
if (!root || root.dataset.dropBound === "1") return;
|
| 15 |
+
|
| 16 |
+
function prevent(e) {
|
| 17 |
+
e.preventDefault();
|
| 18 |
+
e.stopPropagation();
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
function findInput() {
|
| 22 |
+
return root.querySelector('input[type="file"]') || root.querySelector("input");
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function onDrop(e) {
|
| 26 |
+
prevent(e);
|
| 27 |
+
var files = e.dataTransfer && e.dataTransfer.files;
|
| 28 |
+
console.log("[drop] files:", files && files.length);
|
| 29 |
+
if (!files || files.length === 0) return;
|
| 30 |
+
|
| 31 |
+
var input = findInput();
|
| 32 |
+
if (!input) {
|
| 33 |
+
console.warn("[drop] file input not found");
|
| 34 |
+
return;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
var dt = new DataTransfer();
|
| 38 |
+
dt.items.add(files[0]);
|
| 39 |
+
input.files = dt.files;
|
| 40 |
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
root.addEventListener("dragenter", prevent, true);
|
| 44 |
+
root.addEventListener("dragover", prevent, true);
|
| 45 |
+
root.addEventListener("drop", onDrop, true);
|
| 46 |
+
root.dataset.dropBound = "1";
|
| 47 |
+
console.log("[drop] bound");
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
var observer = new MutationObserver(function () {
|
| 51 |
+
bindDrop();
|
| 52 |
+
});
|
| 53 |
+
observer.observe(document.body, { childList: true, subtree: true });
|
| 54 |
+
|
| 55 |
+
window.addEventListener("load", function () {
|
| 56 |
+
bindDrop();
|
| 57 |
+
});
|
| 58 |
+
setTimeout(bindDrop, 1000);
|
| 59 |
+
})();
|
| 60 |
+
</script>
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def on_change(image):
|
| 65 |
+
return "changed" if image is not None else "cleared"
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
with gr.Blocks(css=css) as demo:
|
| 69 |
+
gr.Markdown("Drop replace test")
|
| 70 |
+
img = gr.Image(
|
| 71 |
+
label="Input Image",
|
| 72 |
+
show_label=False,
|
| 73 |
+
type="pil",
|
| 74 |
+
interactive=True,
|
| 75 |
+
elem_id="input-image",
|
| 76 |
+
)
|
| 77 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 78 |
+
img.change(on_change, inputs=img, outputs=status)
|
| 79 |
+
gr.HTML(DROP_JS)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|