@@ -81,3 +81,193 @@ pytest --cov; open ./coverage/html/index.html
8181<!-- #TODO: get input from blessedcoolant here, for the moment inserted the frontend README via snippets extension.-->
8282
8383--8<-- "invokeai/frontend/web/README.md"
84+
85+ ## Developing InvokeAI in VSCode
86+
87+ VSCode offers some nice tools:
88+
89+ - python debugger
90+ - automatic ` venv ` activation
91+ - remote dev (e.g. run InvokeAI on a beefy linux desktop while you type in
92+ comfort on your macbook)
93+
94+ ### Setup
95+
96+ You'll need the
97+ [ Python] ( https://marketplace.visualstudio.com/items?itemName=ms-python.python )
98+ and
99+ [ Pylance] ( https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance )
100+ extensions installed first.
101+
102+ It's also really handy to install the ` Jupyter ` extensions:
103+
104+ - [ Jupyter] ( https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter )
105+ - [ Jupyter Cell Tags] ( https://marketplace.visualstudio.com/items?itemName=ms-toolsai.vscode-jupyter-cell-tags )
106+ - [ Jupyter Notebook Renderers] ( https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter-renderers )
107+ - [ Jupyter Slide Show] ( https://marketplace.visualstudio.com/items?itemName=ms-toolsai.vscode-jupyter-slideshow )
108+
109+ #### InvokeAI workspace
110+
111+ Creating a VSCode workspace for working on InvokeAI is highly recommended. It
112+ can hold InvokeAI-specific settings and configs.
113+
114+ To make a workspace:
115+
116+ - Open the InvokeAI repo dir in VSCode
117+ - ` File ` > ` Save Workspace As ` > save it _ outside_ the repo
118+
119+ #### Default python interpreter (i.e. automatic virtual environment activation)
120+
121+ - Use command palette to run command
122+ ` Preferences: Open Workspace Settings (JSON) `
123+ - Add ` python.defaultInterpreterPath ` to ` settings ` , pointing to your ` venv ` 's
124+ python
125+
126+ Should look something like this:
127+
128+ ``` json
129+ {
130+ // I like to have all InvokeAI-related folders in my workspace
131+ "folders" : [
132+ {
133+ // repo root
134+ "path" : " InvokeAI"
135+ },
136+ {
137+ // InvokeAI root dir, where `invokeai.yaml` lives
138+ "path" : " /path/to/invokeai_root"
139+ }
140+ ],
141+ "settings" : {
142+ // Where your InvokeAI `venv`'s python executable lives
143+ "python.defaultInterpreterPath" : " /path/to/invokeai_root/.venv/bin/python"
144+ }
145+ }
146+ ```
147+
148+ Now when you open the VSCode integrated terminal, or do anything that needs to
149+ run python, it will automatically be in your InvokeAI virtual environment.
150+
151+ Bonus: When you create a Jupyter notebook, when you run it, you'll be prompted
152+ for the python interpreter to run in. This will default to your ` venv ` python,
153+ and so you'll have access to the same python environment as the InvokeAI app.
154+
155+ This is _ super_ handy.
156+
157+ #### Debugging configs with ` launch.json `
158+
159+ Debugging configs are managed in a ` launch.json ` file. Like most VSCode configs,
160+ these can be scoped to a workspace or folder.
161+
162+ Follow the [ official guide] ( https://code.visualstudio.com/docs/python/debugging )
163+ to set up your ` launch.json ` and try it out.
164+
165+ Now we can create the InvokeAI debugging configs:
166+
167+ ``` json
168+ {
169+ // Use IntelliSense to learn about possible attributes.
170+ // Hover to view descriptions of existing attributes.
171+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
172+ "version" : " 0.2.0" ,
173+ "configurations" : [
174+ {
175+ // Run the InvokeAI backend & serve the pre-built UI
176+ "name" : " InvokeAI Web" ,
177+ "type" : " python" ,
178+ "request" : " launch" ,
179+ "program" : " scripts/invokeai-web.py" ,
180+ "args" : [
181+ // Your InvokeAI root dir (where `invokeai.yaml` lives)
182+ " --root" ,
183+ " /path/to/invokeai_root" ,
184+ // Access the app from anywhere on your local network
185+ " --host" ,
186+ " 0.0.0.0"
187+ ],
188+ "justMyCode" : true
189+ },
190+ {
191+ // Run the nodes-based CLI
192+ "name" : " InvokeAI CLI" ,
193+ "type" : " python" ,
194+ "request" : " launch" ,
195+ "program" : " scripts/invokeai-cli.py" ,
196+ "justMyCode" : true
197+ },
198+ {
199+ // Run tests
200+ "name" : " InvokeAI Test" ,
201+ "type" : " python" ,
202+ "request" : " launch" ,
203+ "module" : " pytest" ,
204+ "args" : [" --capture=no" ],
205+ "justMyCode" : true
206+ },
207+ {
208+ // Run a single test
209+ "name" : " InvokeAI Single Test" ,
210+ "type" : " python" ,
211+ "request" : " launch" ,
212+ "module" : " pytest" ,
213+ "args" : [
214+ // Change this to point to the specific test you are working on
215+ " tests/nodes/test_invoker.py"
216+ ],
217+ "justMyCode" : true
218+ },
219+ {
220+ // This is the default, useful to just run a single file
221+ "name" : " Python: File" ,
222+ "type" : " python" ,
223+ "request" : " launch" ,
224+ "program" : " ${file}" ,
225+ "justMyCode" : true
226+ }
227+ ]
228+ }
229+ ```
230+
231+ You'll see these configs in the debugging configs drop down. Running them will
232+ start InvokeAI with attached debugger, in the correct environment, and work just
233+ like the normal app.
234+
235+ Enjoy debugging InvokeAI with ease (not that we have any bugs of course).
236+
237+ #### Remote dev
238+
239+ This is very easy to set up and provides the same very smooth experience as
240+ local development. Environments and debugging, as set up above, just work,
241+ though you'd need to recreate the workspace and debugging configs on the remote.
242+
243+ Consult the
244+ [ official guide] ( https://code.visualstudio.com/docs/remote/remote-overview ) to
245+ get it set up.
246+
247+ Suggest using VSCode's included settings sync so that your remote dev host has
248+ all the same app settings and extensions automagically.
249+
250+ ##### One remote dev gotcha
251+
252+ I've found the automatic port forwarding to be very flakey. You can disable it
253+ in ` Preferences: Open Remote Settings (ssh: hostname) ` . Search for
254+ ` remote.autoForwardPorts ` and untick the box.
255+
256+ To forward ports very reliably, use SSH on the remote dev client (e.g. your
257+ macbook). Here's how to forward both backend API port (` 9090 ` ) and the frontend
258+ live dev server port (` 5173 ` ):
259+
260+ ``` bash
261+ ssh \
262+ -L 9090:localhost:9090 \
263+ -L 5173:localhost:5173 \
264+ user@remote-dev-host
265+ ```
266+
267+ The forwarding stops when you close the terminal window, so suggest to do this
268+ _ outside_ the VSCode integrated terminal in case you need to restart VSCode for
269+ an extension update or something
270+
271+ Now, on your remote dev client, you can open ` localhost:9090 ` and access the UI,
272+ now served from the remote dev host, just the same as if it was running on the
273+ client.
0 commit comments