0

I am doing a project with an html, js and css front-end and a python back-end.
I linked back-end and front-end with the py-script library.
I have to open a json file with the python script and it works, the problem is that it doesn't open the file where i want... it stores the file in this directory: /home/pyodide/file.json

How can I select the location where the file opens/gets created?

by the way, the code I am working with is this:

python
open("my_file.json")
# and it doesn't open it in the same directory as the python file who created it
donnynja
  • 23
  • 6

1 Answers1

1

The file /home/pyodide/file.json is located within the browser virtual file system. That file system is not part of your local disk storage. The virtual file system is not persistent. When you refresh the page any files stored are lost.

There is a set of FS APIs that manage the virtual file system which includes the ability to mount an IDBFS virtual file system for persistent storage. However, that is not the feature you are looking for.

Python applications have the same restrictions that JavaScript applies have. Neither can directly access the local file system. Applications must request access from the web browser and read data from the web browser. This is performed using HTML INPUT elements. The actual file access is performed by the FileRead class.

[UPDATE] Tonight I published an article on Pyscript Files and File Systems that explains in more detail with several examples.

This example is available on my website to experiment with: File Example

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<title>File Example</title>
</head>
<body>

<p>This example shows how to read a file from the local file system and display its contents</p>
<br />
<p>Warning: Not every file type will display. PyScript removes content with tags such as XML, HTML, PHP, etc. Normal text files will work.</p>
<br />
<p>No content type checking is performed to detect images, executables, etc.</p>
<br />
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
<br />
<br />
<div id="print_output"></div>
<br />
<p>File Content:</p>
<div style="border:2px inset #AAA;cursor:text;height:120px;overflow:auto;width:600px; resize:both">
  <div id="content">
  </div>
</div>

<py-script output="print_output">
import asyncio
from js import document, FileReader
from pyodide import create_proxy

async def process_file(event):
    fileList = event.target.files.to_py()

    for f in fileList:
        data = await f.text()
        document.getElementById("content").innerHTML = data

def main():
    # Create a Python proxy for the callback function
    # process_file() is your function to process events from FileReader
    file_event = create_proxy(process_file)

    # Set the listener to the callback
    e = document.getElementById("myfile")
    e.addEventListener("change", file_event, False)

main()
</py-script>

</body>
</html>
John Hanley
  • 74,467
  • 6
  • 95
  • 159