Skip to content

Commit

Permalink
Allow more input types
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jan 15, 2025
1 parent 29ac7a5 commit eea6be2
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ createApp({
script: '',
requirements: '',
output: [],
filesPath: '/home/pyodide/pyla',
filesInputLocation: '/home/pyodide/pyla/input',
filesOutputLocation: '/home/pyodide/pyla/output',
runtimeError: null,
nativefs: null
inputFs: null,
outputFs: null,
}
},
computed: {
Expand All @@ -22,15 +24,23 @@ createApp({
}
return null
},
isUsingFilesystem() {
return this.script.indexOf('FILES_PATH') !== -1
isUsingFilesystemInput() {
return this.script.indexOf('FILES_INPUT_LOCATION') !== -1
},
isUsingFilesystemOutput() {
return this.script.indexOf('FILES_OUTPUT_LOCATION') !== -1
},
isUsingTextInput() {
return this.script.indexOf('TEXT_INPUT') !== -1
}
},
async mounted() {
try {
const pyodide = await window.loadPyodide({
env: {
'FILES_PATH': this.filesPath
'FILES_INPUT_LOCATION': this.filesInputLocation,
'FILES_OUTPUT_LOCATION': this.filesOutputLocation,
'TEXT_INPUT': this.textInput
}
})
this.pyodide = pyodide
Expand All @@ -43,7 +53,11 @@ createApp({
async run () {
try {
if (this.requirements.trim()) {
const requirements = this.requirements.trim().split('\n')
const requirements = this.requirements
.trim()
.split('\n')
.filter((l) => !l.trim().startsWith('#'))

await this.pyodide.loadPackage('micropip')
.then(() => this.pyodide.pyimport('micropip'))
.then(async micropip => {
Expand All @@ -53,22 +67,39 @@ createApp({
})
}

if (this.isUsingFilesystem && !this.nativefs) {
if (this.isUsingFilesystemInput && !this.inputFs) {
const dirHandle = await showDirectoryPicker()
const permissionStatus = await dirHandle.requestPermission({
mode: 'readwrite',
mode: 'read',
})

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

if (this.isUsingFilesystemOutput && !this.outputFs) {
const dirHandle = await showDirectoryPicker()
const permissionStatus = await dirHandle.requestPermission({
mode: 'readwrite',
})

if (permissionStatus !== 'granted') {
throw new Error('read/write access to directory not granted')
}
this.outputFs = await this.pyodide.mountNativeFS(this.filesOutputLocation, dirHandle)
}

let textInput = null
if (this.isUsingTextInput) {
textInput = prompt('Provide text input here')
}

await this.pyodide.runPython(this.script)

if (this.nativefs) {
await this.nativefs.syncfs()
if (this.outputFs) {
await this.outputFs.syncfs()
}
} catch (err) {
this.runtimeError = err
Expand Down

0 comments on commit eea6be2

Please sign in to comment.