Mount a directory from the local filesystem to the virtual filesystem at the specified `path` mount point. The `mode` can be "readwrite" or "read" to specify access level. The `root` parameter provides a hint for the file picker starting location. The `id` parameter allows multiple
(path, mode="readwrite", root="", id="pyscript")
| 65 | |
| 66 | |
| 67 | async def mount(path, mode="readwrite", root="", id="pyscript"): |
| 68 | """ |
| 69 | Mount a directory from the local filesystem to the virtual filesystem |
| 70 | at the specified `path` mount point. The `mode` can be "readwrite" or |
| 71 | "read" to specify access level. The `root` parameter provides a hint |
| 72 | for the file picker starting location. The `id` parameter allows multiple |
| 73 | distinct mounts at the same path. |
| 74 | |
| 75 | On first use, the browser will prompt the user to select a directory |
| 76 | and grant permission. |
| 77 | |
| 78 | ```python |
| 79 | from pyscript import fs |
| 80 | |
| 81 | |
| 82 | # Basic mount with default settings. |
| 83 | await fs.mount("/local") |
| 84 | |
| 85 | # Mount with read-only access. |
| 86 | await fs.mount("/readonly", mode="read") |
| 87 | |
| 88 | # Mount with a hint to start in Downloads folder. |
| 89 | await fs.mount("/downloads", root="downloads") |
| 90 | |
| 91 | # Mount with a custom ID to track different directories. |
| 92 | await fs.mount("/project", id="my-project") |
| 93 | ``` |
| 94 | |
| 95 | If called during a user interaction (like a button click), the |
| 96 | permission dialog may be skipped if permission was previously granted. |
| 97 | """ |
| 98 | js.console.warn("experimental pyscript.fs ⚠️") |
| 99 | |
| 100 | # Check if path is already mounted with a different ID. |
| 101 | mount_key = f"{path}@{id}" |
| 102 | if path in mounted: |
| 103 | # Path already mounted - check if it's the same ID. |
| 104 | for existing_key in mounted.keys(): |
| 105 | if existing_key.startswith(f"{path}@") and existing_key != mount_key: |
| 106 | raise ValueError( |
| 107 | f"Path '{path}' is already mounted with a different ID. " |
| 108 | f"Unmount it first or use a different path." |
| 109 | ) |
| 110 | |
| 111 | details = None |
| 112 | handler = None |
| 113 | |
| 114 | options = {"id": id, "mode": mode} |
| 115 | if root != "": |
| 116 | options["startIn"] = root |
| 117 | |
| 118 | if RUNNING_IN_WORKER: |
| 119 | fs_handler = sync_with_worker.storeFSHandler(mount_key, to_js(options)) |
| 120 | |
| 121 | # Handle both async and SharedArrayBuffer use cases. |
| 122 | if isinstance(fs_handler, bool): |
| 123 | success = fs_handler |
| 124 | else: |