@stlite/desktop 0.49.4 → 0.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/bin/dump_artifacts.js +3 -3
- package/build/asset-manifest.json +3 -3
- package/build/electron/main.js +2 -2
- package/build/electron/preload.js +1 -1
- package/build/index.html +1 -1
- package/build/static/js/3203.d0ce67f2.chunk.js +1 -0
- package/build/static/js/main.f5170537.js +28 -0
- package/package.json +8 -5
- package/wheels/streamlit-1.32.2-cp311-none-any.whl +0 -0
- package/build/static/js/3203.1bd162cb.chunk.js +0 -1
- package/build/static/js/main.2bbf8b09.js +0 -28
- /package/build/static/js/{main.2bbf8b09.js.LICENSE.txt → main.f5170537.js.LICENSE.txt} +0 -0
package/README.md
CHANGED
|
@@ -71,6 +71,25 @@ If you want to hide the toolbar, hamburger menu, and the footer, add the followi
|
|
|
71
71
|
}
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
+
## Data persistence
|
|
75
|
+
|
|
76
|
+
You can mount the IndexedDB-based file system ([`IDBFS`](https://emscripten.org/docs/api_reference/Filesystem-API.html#filesystem-api-idbfs)) to the specified directories by adding the `stlite.desktop.idbfsMountpoints` field to your `package.json`. Note that you have to run the `dump` command again to apply the change.
|
|
77
|
+
|
|
78
|
+
The mounted file system is persistent across the app restarts, while the default file system ([`MEMFS`](https://emscripten.org/docs/api_reference/Filesystem-API.html#memfs)) is ephemeral.
|
|
79
|
+
|
|
80
|
+
In the example below, the IndexedDB-based file system is mounted to the `/mnt` directory so that the files saved in the directory are persistent.
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
{
|
|
84
|
+
// ...other fields...
|
|
85
|
+
"stlite": {
|
|
86
|
+
"desktop": {
|
|
87
|
+
"idbfsMountpoints": ["/mnt"]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
74
93
|
## Limitations
|
|
75
94
|
|
|
76
95
|
- Navigation to external resources like `st.markdown("[link](https://streamlit.io/)")` does not work for security. See https://github.com/whitphx/stlite/pull/445 and let us know if you have use cases where you have to use such external links.
|
package/bin/dump_artifacts.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
var E=Object.create;var T=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var m=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var J=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of H(t))!A.call(e,a)&&a!==i&&T(e,a,{get:()=>t[a],enumerable:!(r=C(t,a))||r.enumerable});return e};var p=(e,t,i)=>(i=e!=null?E(W(e)):{},J(t||!e||!e.__esModule?T(i,"default",{value:e,enumerable:!0}):i,e));var q=m(g=>{"use strict";Object.defineProperty(g,"__esModule",{value:!0});g.verifyRequirements=void 0;function U(e){e.forEach(t=>{let i;try{i=new URL(t)}catch{return}if(i.protocol==="emfs:"||i.protocol==="file:")throw new Error(`"emfs:" and "file:" protocols are not allowed for the requirement (${t})`)})}g.verifyRequirements=U});var O=m(h=>{"use strict";Object.defineProperty(h,"__esModule",{value:!0});h.parseRequirementsTxt=void 0;var L=/\s#.*$/;function z(e){return e.split(`
|
|
3
|
-
`).filter(t=>!t.startsWith("#")).map(t=>t.replace(L,"")).map(t=>t.trim()).filter(t=>t!=="")}h.parseRequirementsTxt=z});var B=m(y=>{"use strict";Object.defineProperty(y,"__esModule",{value:!0});y.PromiseDelegate=void 0;var P=class{constructor(){this.promise=new Promise((t,i)=>{this.resolveInternal=t,this.rejectInternal=i})}resolve(t){this.resolveInternal(t)}reject(t){this.rejectInternal(t)}};y.PromiseDelegate=P});var S=m(c=>{"use strict";var N=c&&c.__createBinding||(Object.create?function(e,t,i,r){r===void 0&&(r=i);var a=Object.getOwnPropertyDescriptor(t,i);(!a||("get"in a?!t.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,r,a)}:function(e,t,i,r){r===void 0&&(r=i),e[r]=t[i]}),v=c&&c.__exportStar||function(e,t){for(var i in e)i!=="default"&&!Object.prototype.hasOwnProperty.call(t,i)&&N(t,e,i)};Object.defineProperty(c,"__esModule",{value:!0});v(q(),c);v(O(),c);v(B(),c)});var F=p(require("yargs")),
|
|
4
|
-
`))}async function G(e){console.info("Copy the build directory (the bare built app files) to this directory...");let t=o.default.resolve(__dirname,K);if(!(await n.default.stat(t)).isDirectory())throw new Error(`The source ${t} does not exist.`);if(t===e.copyTo){console.warn(`sourceDir == destDir (${t}). Are you in the development environment? Skip copying the directory.`);return}if(e.keepOld)try{await n.default.access(e.copyTo),console.info(`${e.copyTo} already exists. Use it and skip copying.`);return}catch{throw new Error(`${e.copyTo} does not exist even though the \`keepOld\` option is specified`)}console.log(`Copy ${t} to ${e.copyTo}`),await n.default.rm(e.copyTo,{recursive:!0,force:!0}),await _.default.copy(t,e.copyTo)}async function Q(e){if(e.requirements.length===0)return[];let t=await(0,b.loadPyodide)();return await x(t,{requirements:e.requirements}),Object.entries(t.loadedPackages).filter(([,i])=>i==="default channel").map(([i])=>i)}async function j(e,t){console.log(`Preparing the local wheel ${t}`);let i=await n.default.readFile(t),r="/tmp/"+o.default.basename(t);e.FS.writeFile(r,i);let a=`emfs:${r}`;return console.log(`The local wheel ${t} is prepared as ${a}`),a}async function x(e,t){await
|
|
3
|
+
`).filter(t=>!t.startsWith("#")).map(t=>t.replace(L,"")).map(t=>t.trim()).filter(t=>t!=="")}h.parseRequirementsTxt=z});var B=m(y=>{"use strict";Object.defineProperty(y,"__esModule",{value:!0});y.PromiseDelegate=void 0;var P=class{constructor(){this.promise=new Promise((t,i)=>{this.resolveInternal=t,this.rejectInternal=i})}resolve(t){this.resolveInternal(t)}reject(t){this.rejectInternal(t)}};y.PromiseDelegate=P});var S=m(c=>{"use strict";var N=c&&c.__createBinding||(Object.create?function(e,t,i,r){r===void 0&&(r=i);var a=Object.getOwnPropertyDescriptor(t,i);(!a||("get"in a?!t.__esModule:a.writable||a.configurable))&&(a={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,r,a)}:function(e,t,i,r){r===void 0&&(r=i),e[r]=t[i]}),v=c&&c.__exportStar||function(e,t){for(var i in e)i!=="default"&&!Object.prototype.hasOwnProperty.call(t,i)&&N(t,e,i)};Object.defineProperty(c,"__esModule",{value:!0});v(q(),c);v(O(),c);v(B(),c)});var F=p(require("yargs")),M=require("yargs/helpers"),o=p(require("path")),n=p(require("fs/promises")),_=p(require("fs-extra")),D=p(require("node-fetch")),b=require("pyodide"),k=p(S());var I=require("pyodide");function w(e){return`https://cdn.jsdelivr.net/pyodide/v${I.version}/full/${e}`}var $=p(require("node-fetch"));var f=class e{static _instance;_data=null;constructor(){}static async loadPyodideBuiltinPackageData(){let t=w("pyodide-lock.json");return console.log(`Load the Pyodide pyodide-lock.json from ${t}`),(await(await(0,$.default)(t,void 0)).json()).packages}static async getInstance(){return this._instance==null&&(this._instance=new e,this._instance._data=await this.loadPyodideBuiltinPackageData()),this._instance}getPackageInfoByName(t){if(this._data==null)throw new Error("The package data is not loaded yet.");let i=Object.values(this._data).find(r=>r.name===t);if(i==null)throw new Error(`Package ${t} is not found in the lock file.`);return i}};global.fetch=D.default;var K="../build",V="../wheels";async function R(e,t){let i=[],r=a=>{i.push(a)};if(await e.loadPackage(t,{errorCallback:r}),i.length>0)throw new Error(i.join(`
|
|
4
|
+
`))}async function G(e){console.info("Copy the build directory (the bare built app files) to this directory...");let t=o.default.resolve(__dirname,K);if(!(await n.default.stat(t)).isDirectory())throw new Error(`The source ${t} does not exist.`);if(t===e.copyTo){console.warn(`sourceDir == destDir (${t}). Are you in the development environment? Skip copying the directory.`);return}if(e.keepOld)try{await n.default.access(e.copyTo),console.info(`${e.copyTo} already exists. Use it and skip copying.`);return}catch{throw new Error(`${e.copyTo} does not exist even though the \`keepOld\` option is specified`)}console.log(`Copy ${t} to ${e.copyTo}`),await n.default.rm(e.copyTo,{recursive:!0,force:!0}),await _.default.copy(t,e.copyTo)}async function Q(e){if(e.requirements.length===0)return[];let t=await(0,b.loadPyodide)();return await x(t,{requirements:e.requirements}),Object.entries(t.loadedPackages).filter(([,i])=>i==="default channel").map(([i])=>i)}async function j(e,t){console.log(`Preparing the local wheel ${t}`);let i=await n.default.readFile(t),r="/tmp/"+o.default.basename(t);e.FS.writeFile(r,i);let a=`emfs:${r}`;return console.log(`The local wheel ${t} is prepared as ${a}`),a}async function x(e,t){await R(e,"micropip");let i=e.pyimport("micropip"),r=[...t.requirements],a=o.default.join(__dirname,V),s=await j(e,o.default.join(a,"stlite_server-0.1.0-py3-none-any.whl"));r.push(s);let l=await j(e,o.default.join(a,"streamlit-1.32.2-cp311-none-any.whl"));r.push(l),console.log("Install the packages:",r),await i.install.callKwargs(r,{keep_going:!0})}async function X(e){console.info("Create the site-packages snapshot file...");let t=await(0,b.loadPyodide)();await R(t,"micropip");let i=t.pyimport("micropip"),r=await f.getInstance(),a=[];e.usedBuiltinPackages.length>0&&(console.log("Mocking builtin packages so that they will not be included in the site-packages snapshot because these will be installed from the vendored wheel files at runtime..."),e.usedBuiltinPackages.forEach(u=>{let d=r.getPackageInfoByName(u);if(d==null)throw new Error(`Package ${u} is not found in the lock file.`);console.log(`Mock ${d.name} ${d.version}`),i.add_mock_package(d.name,d.version),a.push(d.name)})),console.log(`Install the requirements ${JSON.stringify(e.requirements)}`),await x(t,{requirements:e.requirements}),console.log("Remove the mocked packages",a),a.forEach(u=>i.remove_mock_package(u)),console.log("Archive the site-packages director(y|ies)");let s="/tmp/site-packages-snapshot.tar.gz";await t.runPythonAsync(`
|
|
5
5
|
import os
|
|
6
6
|
import tarfile
|
|
7
7
|
import site
|
|
@@ -15,4 +15,4 @@ var E=Object.create;var T=Object.defineProperty;var C=Object.getOwnPropertyDescr
|
|
|
15
15
|
print(os.listdir(site_packages))
|
|
16
16
|
gzf.add(site_packages)
|
|
17
17
|
`),console.log("Extract the archive file from EMFS");let l=t.FS.readFile(s);console.log(`Save the archive file (${e.saveTo})`),await n.default.writeFile(e.saveTo,l)}async function Y(e){console.info("Copy the Streamlit app directory..."),console.log(`Copy ${e.sourceDir} to ${e.copyTo}`),await n.default.rm(e.copyTo,{recursive:!0,force:!0}),await _.default.copy(e.sourceDir,e.copyTo)}async function Z(e){let t=await n.default.readFile(e,{encoding:"utf-8"});return(0,k.parseRequirementsTxt)(t)}async function ee(e,t){let i=t.join(`
|
|
18
|
-
`);await n.default.writeFile(e,i,{encoding:"utf-8"})}async function te(e){let t=await f.getInstance(),r=e.packages.map(a=>t.getPackageInfoByName(a)).map(a=>w(a.file_name));console.log("Downloading the used built-in packages..."),await Promise.all(r.map(async a=>{let s=o.default.resolve(e.destDir,"./pyodide",o.default.basename(a));console.log(`Download ${a} to ${s}`);let l=await(0,D.default)(a);if(!l.ok)throw new Error(`Failed to download ${a}: ${l.status} ${l.statusText}`);let u=await l.arrayBuffer();await n.default.writeFile(s,Buffer.from(u))}))}async function ie(e){let
|
|
18
|
+
`);await n.default.writeFile(e,i,{encoding:"utf-8"})}async function te(e){let t=await f.getInstance(),r=e.packages.map(a=>t.getPackageInfoByName(a)).map(a=>w(a.file_name));console.log("Downloading the used built-in packages..."),await Promise.all(r.map(async a=>{let s=o.default.resolve(e.destDir,"./pyodide",o.default.basename(a));console.log(`Download ${a} to ${s}`);let l=await(0,D.default)(a);if(!l.ok)throw new Error(`Failed to download ${a}: ${l.status} ${l.statusText}`);let u=await l.arrayBuffer();await n.default.writeFile(s,Buffer.from(u))}))}async function ie(e){let i=require(e.packageJsonPath).stlite?.desktop||{},r={embed:i.embed||!1,idbfsMountpoints:i.idbfsMountpoints},a=JSON.stringify(r,null,2);console.log(`Dump the manifest file -> ${e.manifestFilePath}`),console.log(a),await n.default.writeFile(e.manifestFilePath,a,{encoding:"utf-8"})}(0,F.default)((0,M.hideBin)(process.argv)).command("* <appHomeDirSource> [packages..]","Put the user code and data and the snapshot of the required packages into the build artifact.",()=>{},e=>{console.info(e)}).positional("appHomeDirSource",{describe:"The source directory of the user code and data that will be mounted in the Pyodide file system at app runtime",type:"string",demandOption:!0}).positional("packages",{describe:"Package names to install.",type:"string",array:!0}).options("requirement",{describe:"Install from the given requirements file. This option can be used multiple times.",array:!0,type:"string",alias:"r",default:[]}).options("keepOldBuild",{type:"boolean",default:!1,alias:"k",describe:"Keep the existing build directory contents except appHomeDir."}).parseAsync().then(async e=>{let t=process.cwd(),i=o.default.resolve(t,"./build");try{await n.default.access(e.appHomeDirSource)}catch{throw new Error(`${e.appHomeDirSource} does not exist.`)}let r=e.packages??[];for(let s of e.requirement)r=r.concat(await Z(s));(0,k.verifyRequirements)(r);let a=await Q({requirements:r});console.log("The built-in packages loaded for the given requirements:"),console.log(a),await G({copyTo:i,keepOld:e.keepOldBuild}),await X({requirements:r,usedBuiltinPackages:a,saveTo:o.default.resolve(i,"./site-packages-snapshot.tar.gz")}),await ee(o.default.resolve(i,"./requirements.txt"),a),await Y({sourceDir:e.appHomeDirSource,copyTo:o.default.resolve(i,"./streamlit_app")}),await te({packages:a,destDir:i}),await ie({packageJsonPath:o.default.resolve(t,"./package.json"),manifestFilePath:o.default.resolve(i,"./stlite-manifest.json")})});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"files": {
|
|
3
3
|
"main.css": "/static/css/main.fb5f4d99.css",
|
|
4
|
-
"main.js": "/static/js/main.
|
|
5
|
-
"static/js/3203.
|
|
4
|
+
"main.js": "/static/js/main.f5170537.js",
|
|
5
|
+
"static/js/3203.d0ce67f2.chunk.js": "/static/js/3203.d0ce67f2.chunk.js",
|
|
6
6
|
"static/js/4994.37361da4.chunk.js": "/static/js/4994.37361da4.chunk.js",
|
|
7
7
|
"static/js/3685.39209b63.chunk.js": "/static/js/3685.39209b63.chunk.js",
|
|
8
8
|
"static/js/4785.63597083.chunk.js": "/static/js/4785.63597083.chunk.js",
|
|
@@ -162,6 +162,6 @@
|
|
|
162
162
|
},
|
|
163
163
|
"entrypoints": [
|
|
164
164
|
"static/css/main.fb5f4d99.css",
|
|
165
|
-
"static/js/main.
|
|
165
|
+
"static/js/main.f5170537.js"
|
|
166
166
|
]
|
|
167
167
|
}
|
package/build/electron/main.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
2
|
-
`).map(h=>h.trim()).filter(h=>h.length>0)}),
|
|
1
|
+
var v=Object.create;var u=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var w=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of x(t))!y.call(e,a)&&a!==n&&u(e,a,{get:()=>t[a],enumerable:!(s=_(t,a))||s.enumerable});return e};var m=(e,t,n)=>(n=e!=null?v(R(e)):{},w(t||!e||!e.__esModule?u(n,"default",{value:e,enumerable:!0}):n,e)),D=e=>w(u({},"__esModule",{value:!0}),e);var S={};module.exports=D(S);var r=require("electron"),o=m(require("path")),f=m(require("fs/promises"));var c=m(require("fs/promises")),l=m(require("path"));async function P(e){let t={},n=await c.readdir(e);return await Promise.all(n.map(async s=>{let a=l.join(e,s);if((await c.stat(a)).isDirectory()){let i=await P(a);Object.assign(t,i)}else{let i=await c.readFile(a);t[a]=i}})),t}async function g(e){let t=await P(e),n={};return Object.keys(t).forEach(s=>{let d=l.relative(e,s).split(l.sep).join(l.posix.sep);n[d]=t[s]}),n}async function F(){let e=o.resolve(__dirname,"../stlite-manifest.json"),t=await f.readFile(e,{encoding:"utf-8"}),n=JSON.parse(t);return{embed:n.embed??!1,idbfsMountpoints:n.idbfsMountpoints}}var b=async()=>{let e=await F(),t=new r.BrowserWindow({width:1280,height:720,webPreferences:{preload:o.join(__dirname,"preload.js"),sandbox:!0,additionalArguments:e.idbfsMountpoints?[`--idbfs-mountpoints=${JSON.stringify(e.idbfsMountpoints)}`]:void 0}}),n=new URL((r.app.isPackaged,"file:///index.html")),s=new URLSearchParams;e.embed&&s.set("embed","true"),n.search=s.toString();let a=n.toString(),d=i=>i.url===a;r.ipcMain.handle("readSitePackagesSnapshot",i=>{if(!d(i.senderFrame))throw new Error("Invalid IPC sender");let p=o.resolve(__dirname,"../site-packages-snapshot.tar.gz");return f.readFile(p)}),r.ipcMain.handle("readRequirements",async i=>{if(!d(i.senderFrame))throw new Error("Invalid IPC sender");let p=o.resolve(__dirname,"../requirements.txt");return(await f.readFile(p,{encoding:"utf-8"})).split(`
|
|
2
|
+
`).map(h=>h.trim()).filter(h=>h.length>0)}),r.ipcMain.handle("readStreamlitAppDirectory",async i=>{if(!d(i.senderFrame))throw new Error("Invalid IPC sender");let p=o.resolve(__dirname,"../streamlit_app");return g(p)}),t.on("closed",()=>{r.ipcMain.removeHandler("readSitePackagesSnapshot"),r.ipcMain.removeHandler("readRequirements"),r.ipcMain.removeHandler("readStreamlitAppDirectory")}),t.loadURL(a),r.app.isPackaged||t.webContents.openDevTools()};r.app.enableSandbox();r.app.whenReady().then(()=>{let e=o.resolve(__dirname,"..");r.protocol.interceptFileProtocol("file",function(t,n){let s=new URL(t.url).pathname;if(o.isAbsolute(s)){let a=o.join(e,s);n(o.normalize(a))}else n(s)}),b(),r.app.on("activate",()=>{r.BrowserWindow.getAllWindows().length===0&&b()})});r.app.on("window-all-closed",()=>{process.platform!=="darwin"&&r.app.quit()});r.app.on("web-contents-created",(e,t)=>{t.on("will-attach-webview",(n,s,a)=>{n.preventDefault()}),t.on("will-navigate",(n,s)=>{console.debug("will-navigate",s),n.preventDefault()}),t.setWindowOpenHandler(({url:n})=>(console.error("Opening a new window is not allowed."),{action:"deny"}))});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=require("electron")
|
|
1
|
+
var n=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var f=(e,i,s,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let o of d(i))!c.call(e,o)&&o!==s&&n(e,o,{get:()=>i[o],enumerable:!(r=a(i,o))||r.enumerable});return e};var g=e=>f(n({},"__esModule",{value:!0}),e);var k={};module.exports=g(k);var t=require("electron"),p=process.argv.find(e=>e.startsWith("--idbfs-mountpoints="))?.split("=")[1],m=p&&JSON.parse(p),S={idbfsMountpoints:m};t.contextBridge.exposeInMainWorld("appConfig",S);t.contextBridge.exposeInMainWorld("archives",{readSitePackagesSnapshot:()=>t.ipcRenderer.invoke("readSitePackagesSnapshot"),readRequirements:()=>t.ipcRenderer.invoke("readRequirements"),readStreamlitAppDirectory:()=>t.ipcRenderer.invoke("readStreamlitAppDirectory")});
|
package/build/index.html
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>stlite</title><script defer="defer" src="/static/js/main.
|
|
1
|
+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>stlite</title><script defer="defer" src="/static/js/main.f5170537.js"></script><link href="/static/css/main.fb5f4d99.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{"use strict";var e={56977:e=>{function t(e){if("string"!==typeof e)throw new TypeError("Path must be a string. Received "+JSON.stringify(e))}function r(e,t){for(var r,n="",o=0,s=-1,a=0,i=0;i<=e.length;++i){if(i<e.length)r=e.charCodeAt(i);else{if(47===r)break;r=47}if(47===r){if(s===i-1||1===a);else if(s!==i-1&&2===a){if(n.length<2||2!==o||46!==n.charCodeAt(n.length-1)||46!==n.charCodeAt(n.length-2))if(n.length>2){var l=n.lastIndexOf("/");if(l!==n.length-1){-1===l?(n="",o=0):o=(n=n.slice(0,l)).length-1-n.lastIndexOf("/"),s=i,a=0;continue}}else if(2===n.length||1===n.length){n="",o=0,s=i,a=0;continue}t&&(n.length>0?n+="/..":n="..",o=2)}else n.length>0?n+="/"+e.slice(s+1,i):n=e.slice(s+1,i),o=i-s-1;s=i,a=0}else 46===r&&-1!==a?++a:a=-1}return n}var n={resolve:function(){for(var e,n="",o=!1,s=arguments.length-1;s>=-1&&!o;s--){var a;s>=0?a=arguments[s]:(void 0===e&&(e=process.cwd()),a=e),t(a),0!==a.length&&(n=a+"/"+n,o=47===a.charCodeAt(0))}return n=r(n,!o),o?n.length>0?"/"+n:"/":n.length>0?n:"."},normalize:function(e){if(t(e),0===e.length)return".";var n=47===e.charCodeAt(0),o=47===e.charCodeAt(e.length-1);return 0!==(e=r(e,!n)).length||n||(e="."),e.length>0&&o&&(e+="/"),n?"/"+e:e},isAbsolute:function(e){return t(e),e.length>0&&47===e.charCodeAt(0)},join:function(){if(0===arguments.length)return".";for(var e,r=0;r<arguments.length;++r){var o=arguments[r];t(o),o.length>0&&(void 0===e?e=o:e+="/"+o)}return void 0===e?".":n.normalize(e)},relative:function(e,r){if(t(e),t(r),e===r)return"";if((e=n.resolve(e))===(r=n.resolve(r)))return"";for(var o=1;o<e.length&&47===e.charCodeAt(o);++o);for(var s=e.length,a=s-o,i=1;i<r.length&&47===r.charCodeAt(i);++i);for(var l=r.length-i,c=a<l?a:l,g=-1,d=0;d<=c;++d){if(d===c){if(l>c){if(47===r.charCodeAt(i+d))return r.slice(i+d+1);if(0===d)return r.slice(i+d)}else a>c&&(47===e.charCodeAt(o+d)?g=d:0===d&&(g=0));break}var p=e.charCodeAt(o+d);if(p!==r.charCodeAt(i+d))break;47===p&&(g=d)}var u="";for(d=o+g+1;d<=s;++d)d!==s&&47!==e.charCodeAt(d)||(0===u.length?u+="..":u+="/..");return u.length>0?u+r.slice(i+g):(i+=g,47===r.charCodeAt(i)&&++i,r.slice(i))},_makeLong:function(e){return e},dirname:function(e){if(t(e),0===e.length)return".";for(var r=e.charCodeAt(0),n=47===r,o=-1,s=!0,a=e.length-1;a>=1;--a)if(47===(r=e.charCodeAt(a))){if(!s){o=a;break}}else s=!1;return-1===o?n?"/":".":n&&1===o?"//":e.slice(0,o)},basename:function(e,r){if(void 0!==r&&"string"!==typeof r)throw new TypeError('"ext" argument must be a string');t(e);var n,o=0,s=-1,a=!0;if(void 0!==r&&r.length>0&&r.length<=e.length){if(r.length===e.length&&r===e)return"";var i=r.length-1,l=-1;for(n=e.length-1;n>=0;--n){var c=e.charCodeAt(n);if(47===c){if(!a){o=n+1;break}}else-1===l&&(a=!1,l=n+1),i>=0&&(c===r.charCodeAt(i)?-1===--i&&(s=n):(i=-1,s=l))}return o===s?s=l:-1===s&&(s=e.length),e.slice(o,s)}for(n=e.length-1;n>=0;--n)if(47===e.charCodeAt(n)){if(!a){o=n+1;break}}else-1===s&&(a=!1,s=n+1);return-1===s?"":e.slice(o,s)},extname:function(e){t(e);for(var r=-1,n=0,o=-1,s=!0,a=0,i=e.length-1;i>=0;--i){var l=e.charCodeAt(i);if(47!==l)-1===o&&(s=!1,o=i+1),46===l?-1===r?r=i:1!==a&&(a=1):-1!==r&&(a=-1);else if(!s){n=i+1;break}}return-1===r||-1===o||0===a||1===a&&r===o-1&&r===n+1?"":e.slice(r,o)},format:function(e){if(null===e||"object"!==typeof e)throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof e);return function(e,t){var r=t.dir||t.root,n=t.base||(t.name||"")+(t.ext||"");return r?r===t.root?r+n:r+e+n:n}("/",e)},parse:function(e){t(e);var r={root:"",dir:"",base:"",ext:"",name:""};if(0===e.length)return r;var n,o=e.charCodeAt(0),s=47===o;s?(r.root="/",n=1):n=0;for(var a=-1,i=0,l=-1,c=!0,g=e.length-1,d=0;g>=n;--g)if(47!==(o=e.charCodeAt(g)))-1===l&&(c=!1,l=g+1),46===o?-1===a?a=g:1!==d&&(d=1):-1!==a&&(d=-1);else if(!c){i=g+1;break}return-1===a||-1===l||0===d||1===d&&a===l-1&&a===i+1?-1!==l&&(r.base=r.name=0===i&&s?e.slice(1,l):e.slice(i,l)):(0===i&&s?(r.name=e.slice(1,a),r.base=e.slice(1,l)):(r.name=e.slice(i,a),r.base=e.slice(i,l)),r.ext=e.slice(a,l)),i>0?r.dir=e.slice(0,i-1):s&&(r.dir="/"),r},sep:"/",delimiter:":",win32:null,posix:null};n.posix=n,e.exports=n}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n](s,s.exports,r),s.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{function e(e){e.forEach((e=>{let t;try{t=new URL(e)}catch(r){return}if("emfs:"===t.protocol||"file:"===t.protocol)throw new Error(`"emfs:" and "file:" protocols are not allowed for the requirement (${e})`)}))}var t=r(56977),n=r.n(t);function o(e,t){const r=n().normalize(t),o=n().dirname(r).split("/"),s=[];for(const n of o){s.push(n);const t=s.join("/");if(e.FS.analyzePath(t).exists){if(e.FS.isDir(t))throw new Error(`"${t}" already exists and is not a directory.`)}else try{e.FS.mkdir(t)}catch(a){throw console.error(`Failed to create a directory "${t}"`),a}}}function s(e,t,r,n){o(e,t),e.FS.writeFile(t,r,n)}function a(e){e.runPython('\nimport micropip\nmicropip.add_mock_package(\n "pyarrow", "0.0.1",\n modules={\n "pyarrow": """\n__version__ = \'0.0.1\' # TODO: Update when releasing\n\n\nclass Table:\n @classmethod\n def from_pandas(*args, **kwargs):\n raise NotImplementedError("stlite is not supporting this method.")\n"""\n }\n)\n')}var i=function(e,t,r,n){return new(r||(r=Promise))((function(o,s){function a(e){try{l(n.next(e))}catch(t){s(t)}}function i(e){try{l(n.throw(e))}catch(t){s(t)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,i)}l((n=n.apply(e,t||[])).next())}))};let l,c;const g=self,d=new class{constructor(){this.promise=new Promise(((e,t)=>{this.resolveInternal=e,this.rejectInternal=t}))}resolve(e){this.resolveInternal(e)}reject(e){this.rejectInternal(e)}};function p(e){g.postMessage({type:"event:progress",data:{message:e}})}const u="https://cdn.jsdelivr.net/pyodide/v0.25.0/full/pyodide.js";const h=function(){var t,r;return i(this,void 0,void 0,(function*(){const{entrypoint:n,files:o,archives:h,requirements:m,wheels:f,mountedSitePackagesSnapshotFilePath:_,pyodideUrl:y=u,streamlitConfig:v,idbfsMountpoints:b}=yield d.promise;p("Loading Pyodide."),console.debug("Loading Pyodide"),l=yield function(e,t){return i(this,void 0,void 0,(function*(){const r=e.slice(0,e.lastIndexOf("/")+1);let n;return e.endsWith(".mjs")?n=(yield import(e)).loadPyodide:(importScripts(e),n=self.loadPyodide),n(Object.assign(Object.assign({},t),{indexURL:r}))}))}(y,{stdout:console.log,stderr:console.error}),console.debug("Loaded Pyodide");let k=!1;if(b&&(k=!0,b.forEach((e=>{l.FS.mkdir(e),l.FS.mount(l.FS.filesystems.IDBFS,{},e)})),yield new Promise(((e,t)=>{l.FS.syncfs(!0,(r=>{r?t(r):e()}))}))),p("Mounting files."),yield Promise.all(Object.keys(o).map((e=>i(this,void 0,void 0,(function*(){const t=o[e];let r;"url"in t?(console.debug(`Fetch a file from ${t.url}`),r=yield fetch(t.url).then((e=>e.arrayBuffer())).then((e=>new Uint8Array(e)))):r=t.data;const{opts:n}=o[e];console.debug(`Write a file "${e}"`),s(l,e,r,n)}))))),p("Unpacking archives."),yield Promise.all(h.map((e=>i(this,void 0,void 0,(function*(){let t;"url"in e?(console.debug(`Fetch an archive from ${e.url}`),t=yield fetch(e.url).then((e=>e.arrayBuffer()))):t=e.buffer;const{format:r,options:n}=e;console.debug("Unpack an archive",{format:r,options:n}),l.unpackArchive(t,r,n)}))))),!_&&!f)throw new Error("Neither snapshot nor wheel files are provided.");_&&(p("Restoring the snapshot."),yield l.runPythonAsync("import tarfile, shutil, site"),yield l.runPythonAsync("\n site_packages_dirs = site.getsitepackages()\n for site_packages in site_packages_dirs:\n shutil.rmtree(site_packages)\n "),console.debug(`Unarchive ${_}`),yield l.runPythonAsync(`\n with tarfile.open("${_}", "r") as tar_gz_file:\n tar_gz_file.extractall("/")\n `),console.debug("Restored the snapshot"),p("Mocking some packages."),console.debug("Mock pyarrow"),a(l),console.debug("Mocked pyarrow")),e(m),p("Installing packages."),yield l.loadPackage("micropip");const w=l.pyimport("micropip");f?(console.debug("Installing the wheels:",f,"and the requirements:",m),yield w.install.callKwargs([f.stliteServer,f.streamlit,...m],{keep_going:!0}),console.debug("Installed the wheels and the requirements"),p("Mocking some packages."),console.debug("Mock pyarrow"),a(l),console.debug("Mocked pyarrow")):(console.debug("Installing the requirements:",m),yield w.install.callKwargs(m,{keep_going:!0}),console.debug("Installed the requirements")),yield l.runPythonAsync("\n import importlib\n importlib.invalidate_caches()\n "),p("Loading streamlit package."),console.debug("Loading the Streamlit package"),yield l.runPythonAsync("\n import streamlit.runtime\n "),console.debug("Loaded the Streamlit package"),p("Setting up the loggers."),console.debug("Setting the loggers"),yield l.runPythonAsync('\n import logging\n import streamlit.logger\n\n streamlit.logger.get_logger = logging.getLogger\n streamlit.logger.setup_formatter = None\n streamlit.logger.update_formatter = lambda *a, **k: None\n streamlit.logger.set_log_level = lambda *a, **k: None\n\n for name in streamlit.logger._loggers.keys():\n if name == "root":\n name = "streamlit"\n logger = logging.getLogger(name)\n logger.propagate = True\n logger.handlers.clear()\n logger.setLevel(logging.NOTSET)\n\n streamlit.logger._loggers = {}\n ');self.__logCallback__=(e,t)=>{e>=40?console.error(t):e>=30?console.warn(t):e>=20?console.info(t):console.debug(t)},yield l.runPythonAsync('\ndef setup_loggers(streamlit_level, streamlit_message_format):\n from js import __logCallback__\n\n\n class JsHandler(logging.Handler):\n def emit(self, record):\n msg = self.format(record)\n __logCallback__(record.levelno, msg)\n\n\n root_message_format = "%(levelname)s:%(name)s:%(message)s"\n\n root_logger = logging.getLogger()\n root_logger.handlers.clear()\n root_formatter = logging.Formatter(root_message_format)\n root_handler = JsHandler()\n root_handler.setFormatter(root_formatter)\n root_logger.addHandler(root_handler)\n root_logger.setLevel(logging.DEBUG)\n\n streamlit_logger = logging.getLogger("streamlit")\n streamlit_logger.propagate = False\n streamlit_logger.handlers.clear()\n streamlit_formatter = logging.Formatter(streamlit_message_format)\n streamlit_handler = JsHandler()\n streamlit_handler.setFormatter(streamlit_formatter)\n streamlit_logger.addHandler(streamlit_handler)\n streamlit_logger.setLevel(streamlit_level.upper())\n ');const S=(null!==(t=null===v||void 0===v?void 0:v["logger.level"])&&void 0!==t?t:"INFO").toString(),A=null!==(r=null===v||void 0===v?void 0:v["logger.messageFormat"])&&void 0!==r?r:"%(asctime)s %(message)s";if(l.globals.get("setup_loggers")(S,A),console.debug("Set the loggers"),p("Mocking some Streamlit functions for the browser environment."),console.debug("Mocking some Streamlit functions"),yield l.runPythonAsync("\n import streamlit\n\n def is_cacheable_msg(msg):\n return False\n\n streamlit.runtime.runtime.is_cacheable_msg = is_cacheable_msg\n "),console.debug("Mocked some Streamlit functions"),k){p("Setting up the IndexedDB filesystem synchronizer."),console.debug("Setting up the IndexedDB filesystem synchronizer");let e=!1;self.__scriptFinishedCallback__=()=>{console.debug("The script has finished. Syncing the filesystem."),e||(e=!0,l.FS.syncfs(!1,(t=>{e=!1,t&&console.error(t)})))},yield l.runPythonAsync('\nfrom streamlit.runtime.app_session import AppSession\nfrom streamlit.runtime.scriptrunner import ScriptRunnerEvent\nfrom js import __scriptFinishedCallback__\n\ndef wrap_app_session_on_scriptrunner_event(original_method):\n def wrapped(self, *args, **kwargs):\n if "event" in kwargs:\n event = kwargs["event"]\n if event == ScriptRunnerEvent.SCRIPT_STOPPED_WITH_SUCCESS or event == ScriptRunnerEvent.SCRIPT_STOPPED_FOR_RERUN or event == ScriptRunnerEvent.SHUTDOWN:\n __scriptFinishedCallback__()\n return original_method(self, *args, **kwargs)\n return wrapped\n\nAppSession._on_scriptrunner_event = wrap_app_session_on_scriptrunner_event(AppSession._on_scriptrunner_event)\n '),console.debug("Set up the IndexedDB filesystem synchronizer")}p("Booting up the Streamlit server."),console.debug("Booting up the Streamlit server"),self.__streamlitFlagOptions__=Object.assign(Object.assign({"browser.gatherUsageStats":!1},v),{"runner.fastReruns":!1}),yield l.runPythonAsync(`\n from stlite_server.bootstrap import load_config_options, prepare\n from stlite_server.server import Server\n from js import __streamlitFlagOptions__\n\n flag_options = __streamlitFlagOptions__.to_py()\n load_config_options(flag_options)\n\n main_script_path = "${n}"\n command_line = None\n args = []\n\n prepare(main_script_path, args)\n\n server = Server(main_script_path, command_line)\n server.start()\n `),console.debug("Booted up the Streamlit server"),console.debug("Setting up the HTTP server"),c=l.globals.get("server").copy(),console.debug("Set up the HTTP server"),g.postMessage({type:"event:loaded"})}))}().catch((e=>{throw g.postMessage({type:"event:error",data:{error:e}}),e}));self.onmessage=t=>i(void 0,void 0,void 0,(function*(){const r=t.data;if("initData"===r.type)return void d.resolve(r.data);yield h;const n=t.ports[0];try{switch(r.type){case"websocket:connect":{console.debug("websocket:connect",r.data);const{path:e}=r.data;c.start_websocket(e,((e,t)=>{if(t){const t=e.getBuffer("u8");e.destroy();const r=new Uint8ClampedArray(t.data.buffer,t.data.byteOffset,t.data.byteLength);g.postMessage({type:"websocket:message",data:{payload:new Uint8Array(r)}})}else g.postMessage({type:"websocket:message",data:{payload:e}})})),n.postMessage({type:"reply"});break}case"websocket:send":{console.debug("websocket:send",r.data);const{payload:e}=r.data;c.receive_websocket_from_js(e);break}case"http:request":{console.debug("http:request",r.data);const{request:e}=r.data,t=(e,t,r)=>{const o=t.toJs(),s=r.toJs();console.debug({statusCode:e,headers:o,body:s});const a={type:"http:response",data:{response:{statusCode:e,headers:o,body:s}}};n.postMessage(a)};c.receive_http_from_js(e.method,decodeURIComponent(e.path),e.headers,e.body,t);break}case"file:write":{const{path:e,data:t,opts:o}=r.data;console.debug(`Write a file "${e}"`),s(l,e,t,o),n.postMessage({type:"reply"});break}case"file:rename":{const{oldPath:e,newPath:t}=r.data;console.debug(`Rename "${e}" to ${t}`),function(e,t,r){o(e,r),e.FS.rename(t,r)}(l,e,t),n.postMessage({type:"reply"});break}case"file:unlink":{const{path:e}=r.data;console.debug(`Remove "${e}`),l.FS.unlink(e),n.postMessage({type:"reply"});break}case"install":{const{requirements:t}=r.data,o=l.pyimport("micropip");console.debug("Install the requirements:",t),e(t),yield o.install.callKwargs(t,{keep_going:!0}).then((()=>{if(t.includes("matplotlib"))return l.runPythonAsync("\n from stlite_server.bootstrap import _fix_matplotlib_crash\n _fix_matplotlib_crash()\n ")})).then((()=>{console.debug("Successfully installed"),n.postMessage({type:"reply"})}))}}}catch(a){if(console.error(a),!(a instanceof Error))throw a;const e=new Error(a.message);e.name=a.name,e.stack=a.stack,n.postMessage({type:"reply",error:e})}})),g.postMessage({type:"event:start"})})()})();
|