It sounds like you are trying to make an HTML and JavaScript app that achieves two objectives.
- Display a file picker
- Run a set of Python commands
I would recommend you use Electron for this. It allows you to create HTML and JavaScript apps that can interact with terminal commands like Python.
Make sure to get Node.js installed first to use Electron.
To start you will need the Electron Quick Start. I have linked it on GitHub and you can get it like this.
# Clone this repository
git clone https://github.com/electron/electron-quick-start
# Go into the repository
cd electron-quick-start
# Install dependencies
npm install
Within the folder you will find the familiar index.html
file. To add a file picker to your app use the below HTML. You can read more about it on the MDN docs.
<input type="file" id="myFilePicker">
In order to have your Python commands run when a file is picked, you can make use of the preload.js
file included with the Quick Start. This JavaScript can interact with your HTML and also your computer. Replace it's contents with the below code.
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('myFilePicker').addEventListener('change', function(event) {
let filePath = this.files[0].path
const spawn = require("child_process").spawn;
const pythonProcess = spawn('python',["path/to/script.py", filePath]);
pythonProcess.stdout.on('data', (data) => {
// You have data from Python
});
})
})
This JavaScript does the following:
- Waits for the HTML to load
- Listens for the file picker to change
- Gets the path to the file that was chosen
- Runs a python script, passing the file path as a parameter.
The Python script code was used from this answer. Within the arrow function that says You have data from Python
, you are free to repeat the Python script code. You can then run your second and third scripts.
You can then run npm start
in the terminal to launch your new Electron app.