|
24 | 24 | "cell_type": "markdown",
|
25 | 25 | "metadata": {},
|
26 | 26 | "source": [
|
27 |
| - "##### In this tutorial, we will complete the task using `PythonOPTemplate` and function OP" |
| 27 | + "##### In this tutorial, we will complete the task using function OP" |
28 | 28 | ]
|
29 | 29 | },
|
30 | 30 | {
|
|
37 | 37 | },
|
38 | 38 | {
|
39 | 39 | "cell_type": "code",
|
40 |
| - "execution_count": 1, |
| 40 | + "execution_count": null, |
41 | 41 | "metadata": {},
|
42 | 42 | "outputs": [],
|
43 | 43 | "source": [
|
| 44 | + "import sys\n", |
44 | 45 | "from pathlib import Path\n",
|
45 | 46 | "\n",
|
46 |
| - "from dflow import Step, Workflow\n", |
47 |
| - "from dflow.python import OP, Artifact, PythonOPTemplate" |
| 47 | + "from dflow import Workflow\n", |
| 48 | + "from dflow.python import OP, Artifact" |
48 | 49 | ]
|
49 | 50 | },
|
50 | 51 | {
|
|
54 | 55 | "source": [
|
55 | 56 | "For step 1: \n",
|
56 | 57 | "\n",
|
57 |
| - "This class can achieve the function to write files. In the example, we try to write a file containing message of string format, and output a number of int format.\n", |
| 58 | + "This OP is to write files. In the example, we try to write a file containing message of string format, and output a number of int format.\n", |
58 | 59 | "- input:\n",
|
59 | 60 | " - \"msg\": the input message\n",
|
60 | 61 | "- output:\n",
|
|
64 | 65 | },
|
65 | 66 | {
|
66 | 67 | "cell_type": "code",
|
67 |
| - "execution_count": 2, |
| 68 | + "execution_count": null, |
68 | 69 | "metadata": {},
|
69 | 70 | "outputs": [],
|
70 | 71 | "source": [
|
71 |
| - "@OP.function\n", |
72 |
| - "def WriteFile(msg: str) -> {\"out_art\": Artifact(Path), \"length\": int}:\n", |
| 72 | + "@OP.function(image=f\"python:{sys.version_info.major}.{sys.version_info.minor}\")\n", |
| 73 | + "def write_file(msg: str) -> {\"out_art\": Artifact(Path), \"length\": int}:\n", |
73 | 74 | " with open(\"msg.txt\",\"w\") as f:\n",
|
74 | 75 | " f.write(msg)\n",
|
75 | 76 | " \n",
|
|
91 | 92 | },
|
92 | 93 | {
|
93 | 94 | "cell_type": "code",
|
94 |
| - "execution_count": 3, |
| 95 | + "execution_count": null, |
95 | 96 | "metadata": {},
|
96 | 97 | "outputs": [],
|
97 | 98 | "source": [
|
98 |
| - "@OP.function\n", |
99 |
| - "def Duplicate(in_art: Artifact(Path), in_num: int) -> {\"out_art\": Artifact(Path), \"out_num\": int}:\n", |
| 99 | + "@OP.function(image=f\"python:{sys.version_info.major}.{sys.version_info.minor}\")\n", |
| 100 | + "def duplicate(in_art: Artifact(Path), in_num: int) -> {\"out_art\": Artifact(Path), \"out_num\": int}:\n", |
100 | 101 | " with open(in_art, \"r\") as f:\n",
|
101 | 102 | " content = f.read()\n",
|
102 | 103 | " with open(\"bar.txt\", \"w\") as f:\n",
|
|
113 | 114 | "cell_type": "markdown",
|
114 | 115 | "metadata": {},
|
115 | 116 | "source": [
|
116 |
| - "After defining the PythonOPTemplate, it is to define steps.\n", |
117 |
| - "- step0 is to write a file and to output length of the content using WriteFile OP and python image\n", |
118 |
| - " - parameters:\n", |
119 |
| - " - \"msg\": \"HelloWorld!\"\n", |
120 |
| - "- step1 is to duplicate the content in the file and to duplicate the number using Duplicate OP and python image\n", |
121 |
| - " - artifacts:\n", |
122 |
| - " - in_art file is from step0.outputs.artifacts[\"out_art\"]\n", |
123 |
| - " - parameters:\n", |
124 |
| - " - in_num is from step0.outputs.parameters[\"length\"]\n", |
125 |
| - "\n", |
126 |
| - "Finally, we need to set up a Workflow named \"python\" and then add step0 and step1.\n", |
127 |
| - "\n", |
128 |
| - "`wf.submit` is to submit this workflow to Argo." |
| 117 | + "After defining OPs, call the OPs in series in the context of a workflow, which will connect them together as a workflow and submit it finally." |
129 | 118 | ]
|
130 | 119 | },
|
131 | 120 | {
|
132 | 121 | "cell_type": "code",
|
133 |
| - "execution_count": 4, |
| 122 | + "execution_count": null, |
134 | 123 | "metadata": {},
|
135 |
| - "outputs": [ |
136 |
| - { |
137 |
| - "name": "stdout", |
138 |
| - "output_type": "stream", |
139 |
| - "text": [ |
140 |
| - "Workflow has been submitted (ID: python-wxj66, UID: 600f99da-21ee-40af-9a3b-c738e46556b8)\n" |
141 |
| - ] |
142 |
| - } |
143 |
| - ], |
| 124 | + "outputs": [], |
144 | 125 | "source": [
|
145 |
| - "import sys\n", |
146 |
| - "step0 = Step(\n", |
147 |
| - " name=\"step0\",\n", |
148 |
| - " template=PythonOPTemplate(WriteFile, image=f\"python:{sys.version_info.major}.{sys.version_info.minor}\"),\n", |
149 |
| - " parameters={\"msg\": \"HelloWorld!\"},\n", |
150 |
| - ")\n", |
151 |
| - "\n", |
152 |
| - "step1 = Step(\n", |
153 |
| - " name=\"step1\",\n", |
154 |
| - " template=PythonOPTemplate(Duplicate, image=f\"python:{sys.version_info.major}.{sys.version_info.minor}\"),\n", |
155 |
| - " parameters={\"in_num\": step0.outputs.parameters[\"length\"]},\n", |
156 |
| - " artifacts={\"in_art\": step0.outputs.artifacts[\"out_art\"]},\n", |
157 |
| - ")\n", |
158 |
| - "wf = Workflow(name=\"python\")\n", |
159 |
| - "wf.add(step0)\n", |
160 |
| - "wf.add(step1)\n", |
161 |
| - "wf.submit();" |
| 126 | + "with Workflow(name=\"python\") as wf:\n", |
| 127 | + " out = write_file(msg=\"HelloWorld!\")\n", |
| 128 | + " duplicate(in_num=out[\"length\"], in_art=out[\"out_art\"])" |
162 | 129 | ]
|
163 | 130 | }
|
164 | 131 | ],
|
|
0 commit comments