|
| 1 | +:orphan: |
| 2 | + |
| 3 | + |
| 4 | +************ |
| 5 | +About Drives |
| 6 | +************ |
| 7 | + |
| 8 | +Lightning Drive storage makes it easy to share files between LightningWorks so you can run your Lightning App both locally and in the cloud without changing the code. |
| 9 | + |
| 10 | +The Drive object provides a central place for your components to share data. |
| 11 | + |
| 12 | +The Drive acts as an isolate folder and any component can access it by knowing its name. |
| 13 | + |
| 14 | +Your components can put, list, get, and delete files from and to the Drive (except LightningFlows). |
| 15 | + |
| 16 | +---- |
| 17 | + |
| 18 | +*********************** |
| 19 | +What Drive does for you |
| 20 | +*********************** |
| 21 | + |
| 22 | +Think of every instance of the Drive object acting like a Google Drive or like Dropbox. |
| 23 | + |
| 24 | +By sharing the Drive between components through the LightningFlow, |
| 25 | +several components can have a shared place to read and write files from. |
| 26 | + |
| 27 | +---- |
| 28 | + |
| 29 | +************** |
| 30 | +Create a Drive |
| 31 | +************** |
| 32 | + |
| 33 | +In order to create a Drive, you simply need to pass its name with the prefix ``lit://`` as follows: |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + from lightning_app.storage import Drive |
| 38 | +
|
| 39 | + # The identifier of this Drive is ``drive_1`` |
| 40 | + # Note: You need to add Lightning protocol ``lit://`` as a prefix. |
| 41 | +
|
| 42 | + drive_1 = Drive("lit://drive_1") |
| 43 | +
|
| 44 | + # The identifier of this Drive is ``drive_2`` |
| 45 | + drive_2 = Drive("lit://drive_2") |
| 46 | +
|
| 47 | +Any components can create a drive object. |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + from lightning_app import LightningFlow, LightningWork |
| 52 | + from lightning_app.storage import Drive |
| 53 | +
|
| 54 | +
|
| 55 | + class Flow(LightningFlow): |
| 56 | + def __init__(self): |
| 57 | + super().__init__() |
| 58 | + self.drive_1 = Drive("lit://drive_1") |
| 59 | +
|
| 60 | + def run(self): |
| 61 | + ... |
| 62 | +
|
| 63 | +
|
| 64 | + class Work(LightningWork): |
| 65 | + def __init__(self): |
| 66 | + super().__init__() |
| 67 | + self.drive_1 = Drive("lit://drive_1") |
| 68 | +
|
| 69 | + def run(self): |
| 70 | + ... |
| 71 | +
|
| 72 | +---- |
| 73 | + |
| 74 | +***************************** |
| 75 | +Supported actions with Drives |
| 76 | +***************************** |
| 77 | + |
| 78 | +A Drive supports put, list, get, and delete actions. |
| 79 | + |
| 80 | +.. code-block:: python |
| 81 | +
|
| 82 | + from lightning_app.storage import Drive |
| 83 | +
|
| 84 | + drive = Drive("lit://drive") |
| 85 | +
|
| 86 | + drive.list(".") # Returns [] as empty |
| 87 | +
|
| 88 | + # Created file. |
| 89 | + with open("a.txt", "w") as f: |
| 90 | + f.write("Hello World !") |
| 91 | +
|
| 92 | + drive.put("a.txt") |
| 93 | +
|
| 94 | + drive.list(".") # Returns ["a.txt"] as the file copied in the Drive during the put action. |
| 95 | +
|
| 96 | + drive.get("a.txt") # Get the file into the current worker |
| 97 | +
|
| 98 | + drive.delete("a.txt") |
| 99 | +
|
| 100 | + drive.list(".") # Returns [] as empty |
| 101 | +
|
| 102 | +---- |
| 103 | + |
| 104 | +********************************** |
| 105 | +Component interactions with Drives |
| 106 | +********************************** |
| 107 | + |
| 108 | +Here is an illustrated code example on how to create drives within works. |
| 109 | + |
| 110 | +.. figure:: https://pl-flash-data.s3.amazonaws.com/assets_lightning/drive_2.png |
| 111 | + |
| 112 | +.. code-block:: python |
| 113 | +
|
| 114 | + from lightning_app import LightningFlow, LightningWork |
| 115 | + from lightning_app.core.app import LightningApp |
| 116 | + from lightning_app.storage.drive import Drive |
| 117 | +
|
| 118 | +
|
| 119 | + class Work_A(LightningWork): |
| 120 | + def __init__(self): |
| 121 | + super().__init__() |
| 122 | + # The identifier of the Drive is ``drive_1`` |
| 123 | + # Note: You need to add Lightning protocol ``lit://`` as a prefix. |
| 124 | + self.drive_1 = Drive("lit://drive_1") |
| 125 | +
|
| 126 | + def run(self): |
| 127 | + # 1. Create a file. |
| 128 | + with open("a.txt", "w") as f: |
| 129 | + f.write("Hello World !") |
| 130 | +
|
| 131 | + # 2. Put the file into the drive. |
| 132 | + self.drive_1.put("a.txt") |
| 133 | +
|
| 134 | +
|
| 135 | + class Work_B(LightningWork): |
| 136 | + def __init__(self): |
| 137 | + super().__init__() |
| 138 | +
|
| 139 | + # Note: Work B has access 2 drives. |
| 140 | +
|
| 141 | + # The identifier of this Drive is ``drive_1`` |
| 142 | + self.drive_1 = Drive("lit://drive_1") |
| 143 | + # The identifier of this Drive is ``drive_2`` |
| 144 | + self.drive_2 = Drive("lit://drive_2") |
| 145 | +
|
| 146 | + def run(self): |
| 147 | + # 1. Create a file. |
| 148 | + with open("b.txt", "w") as f: |
| 149 | + f.write("Hello World !") |
| 150 | +
|
| 151 | + # 2. Put the file into both drives. |
| 152 | + self.drive_1.put("b.txt") |
| 153 | + self.drive_2.put("b.txt") |
| 154 | +
|
| 155 | +
|
| 156 | + class Work_C(LightningWork): |
| 157 | + def __init__(self): |
| 158 | + super().__init__() |
| 159 | + self.drive_2 = Drive("lit://drive_2") |
| 160 | +
|
| 161 | + def run(self): |
| 162 | + # 1. Create a file. |
| 163 | + with open("c.txt", "w") as f: |
| 164 | + f.write("Hello World !") |
| 165 | +
|
| 166 | + # 2. Put the file into the drive. |
| 167 | + self.drive_2.put("c.txt") |
| 168 | +
|
| 169 | +---- |
| 170 | + |
| 171 | +***************************** |
| 172 | +Transfer files with Drive |
| 173 | +***************************** |
| 174 | + |
| 175 | +In the example below, the Drive is created by the flow and passed to its LightningWork's. |
| 176 | + |
| 177 | +The ``Work_1`` put a file **a.txt** in the **Drive("lit://this_drive_id")** and the ``Work_2`` can list and get the **a.txt** file from it. |
| 178 | + |
| 179 | +.. literalinclude:: ../../../examples/app_drive/app.py |
| 180 | + |
| 181 | + |
| 182 | +---- |
| 183 | + |
| 184 | +.. raw:: html |
| 185 | + |
| 186 | + <div class="display-card-container"> |
| 187 | + <div class="row"> |
| 188 | + |
| 189 | +.. displayitem:: |
| 190 | + :header: Learn about the Path Object. |
| 191 | + :description: Transfer Files From One Component to Another by Reference. |
| 192 | + :col_css: col-md-4 |
| 193 | + :button_link: path.html |
| 194 | + :height: 180 |
| 195 | + :tag: Intermediate |
| 196 | + |
| 197 | +.. raw:: html |
| 198 | + |
| 199 | + </div> |
| 200 | + </div> |
0 commit comments