-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (60 loc) · 2.11 KB
/
Copy pathscript.js
File metadata and controls
71 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
async function main() {
let submitBtn = document.getElementById("submitBtn");
submitBtn.disabled = true;
submitBtn.innerHTML = "Loading dependencies...";
statuShowOutput(false);
let pyodide = await loadPyodide();
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
// pdfplumber
pyodide.loadPackage(
"https://files.pythonhosted.org/packages/6e/4c/5abaf9668b22628cdd899ef863a2c3e62bba316402e3313ab661608ae44e/pdfplumber-0.8.0-py3-none-any.whl"
);
await micropip.install("pdfminer.six");
pyodide.globals.set("buildCarrousel", buildCarrousel);
window.pyodide = pyodide;
submitBtn.disabled = false;
submitBtn.innerHTML = "Magic!";
}
main();
function statuShowOutput(show) {
let output = document.getElementById("output");
output.style.display = show ? "block" : "none";
}
function plumb() {
let fileInput = document.querySelector("input#filePDF");
let file = fileInput.files[0];
if (file && file.type === "application/pdf") {
let reader = new FileReader();
reader.onload = async function (e) {
let contents = e.target.result.split(",")[1];
// Fetch the Python code from the file
let pythonCodeResponse = await fetch("python.txt");
let pythonCode = await pythonCodeResponse.text();
pythonCode = pythonCode.replace("${contents}", contents);
pythonCode = pythonCode.replace("${file.name}", file.name);
window.pyodide.runPythonAsync(pythonCode);
};
reader.readAsDataURL(file);
} else {
alert("A PDF has not been uploaded.");
}
}
function buildCarrousel(pages) {
const carousel = document.getElementById("carousel");
carousel.innerHTML = "";
pages.forEach((text, index) => {
const pageElement = document.createElement("div");
pageElement.className = "page";
pageElement.innerHTML = `<h2>Page ${index + 1}</h2><p>${text}</p>`;
carousel.appendChild(pageElement);
});
carousel.scrollLeft = 0;
}
function scrollCarousel(direction) {
const carousel = document.getElementById("carousel");
carousel.scrollBy({
left: direction * (carousel.clientWidth - 20),
behavior: "smooth",
});
}