Skip to content

Commit

Permalink
Scripts that do not need fs access should not prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Dec 21, 2024
1 parent f8ed018 commit 17763fa
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ createApp({
pyodide: null,
script: '',
requirements: '',
output: ['Output goes here ...']
output: ['Output goes here ...'],
filesPath: '/home/pyodide/pyla'
}
},
computed: {
Expand All @@ -15,13 +16,16 @@ createApp({
return this.pyodide
}
return null
},
isUsingFilesystem() {
return this.script.indexOf('FILES_PATH') !== -1
}
},
async mounted() {
try {
const pyodide = await window.loadPyodide({
env: {
'FILES_PATH': '/home/pyodide/pyla'
'FILES_PATH': this.filesPath
}
})
this.pyodide = pyodide
Expand All @@ -32,6 +36,8 @@ createApp({
},
methods: {
async run () {
this.output = []

if (this.requirements.trim()) {
const requirements = this.requirements.trim().split('\n')
await this.pyodide.loadPackage('micropip')
Expand All @@ -42,16 +48,25 @@ createApp({
}
})
}
const dirHandle = await showDirectoryPicker()
const permissionStatus = await dirHandle.requestPermission({
mode: 'readwrite',
})

if (permissionStatus !== 'granted') {
throw new Error('read access to directory not granted')
let nativefs
if (this.isUsingFilesystem) {
const dirHandle = await showDirectoryPicker()
const permissionStatus = await dirHandle.requestPermission({
mode: 'readwrite',
})

if (permissionStatus !== 'granted') {
throw new Error('read access to directory not granted')
}
nativefs = await this.pyodide.mountNativeFS(this.filesPath, dirHandle)
}
const nativefs = await this.pyodide.mountNativeFS('/home/pyodide/pyla', dirHandle)

await this.pyodide.runPython(this.script)

if (nativefs) {
await nativefs.syncfs()
}
}
}
}).mount('#app')

0 comments on commit 17763fa

Please sign in to comment.