Wing Tipshttps://wingware.com/Tips, tricks, and helpful hints for Wingware's Python IDEsThu, 05 Dec 2019 23:39:59 GMTPyRSS2Gen-1.0.0http://www.dalkescientific.com/Python/PyRSS2Gen.htmlNavigating Python Code with Wing Pro 7 (part 3 of 3)https://wingware.com/blog/code-navigation-3<p><a class="reference" href="/hints/code-navigation-2">Last week</a> and the <a class="reference" href="/hints/code-navigation-1">week before</a>, we looked at some of the code navigation features in Wing, including goto-definition, find uses, and project-wide search, code index menus, and the Source Browser.</p> <p>This week we'll finish up this mini-series by looking at how to quickly and easily find and open files or visit symbols in Python code by typing a name fragment.</p> <div class="section"> <h3 class="title-3">Project Configuration</h3> <p>The features described here assume that you have used <tt class="literal"><span class="pre">Add</span> <span class="pre">Existing</span> <span class="pre">Directory</span></tt> in the <tt class="literal"><span class="pre">Project</span></tt> menu to add your source code to your project. Typically the project should contain the code you are actively working on. Packages that your code uses can be left out of the project, unless you anticipate often wanting to open or search files in them. Wing will still be able to find them through the Python Path, as needed for auto-completion, code warnings, and other purposes.</p> </div> <div class="section"> <h3 class="title-3">Open From Project</h3> <p><tt class="literal"><span class="pre">Open</span> <span class="pre">from</span> <span class="pre">Project</span></tt> from the <tt class="literal"><span class="pre">File</span></tt> menu is typically the easiest way to navigate to a file by name. This displays a dialog that lists the project files whose names match a fragment:</p> <img src="https://wingware.com/images/blog/code-navigation/open-from-project.png" alt="/images/blog/code-navigation/open-from-project.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="500px" /><p>Fragments can be abbreviations of the file name and may match enclosing directory names if they contain <tt class="literal"><span class="pre">/</span></tt> or <tt class="literal"><span class="pre">\</span></tt>. The arrow keys navigate the list and pressing <tt class="literal"><span class="pre">Enter</span></tt> opens the selected file.</p> </div> <div class="section"> <h3 class="title-3">Find Symbol</h3> <p>A similar interface is available to find Python code symbols by name. For the current file, this is <tt class="literal"><span class="pre">Find</span> <span class="pre">Symbol</span></tt> in the <tt class="literal"><span class="pre">Source</span></tt> menu. For all project files, use <tt class="literal"><span class="pre">Find</span> <span class="pre">Symbol</span> <span class="pre">in</span> <span class="pre">Project</span></tt> instead:</p> <img src="https://wingware.com/images/blog/code-navigation/find-symbol-in-project.png" alt="/images/blog/code-navigation/find-symbol-in-project.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="515px" /><br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> <p>As always, please don't hesitate to email <a class="reference" href="mailto:support&#64;wingware.com">support&#64;wingware.com</a> if you run into problems or have any questions.</p> </div> https://wingware.com/blog/code-navigation-3Thu, 28 Nov 2019 01:00:00 GMTNavigating Python Code with Wing Pro 7 (part 2 of 3)https://wingware.com/blog/code-navigation-2<p><a class="reference" href="/hints/code-navigation-1">Last week</a> we looked at goto-definition, find uses, and project-wide search as tools for navigating Python code in Wing 7. This time, we'll take a look at the code indices that Wing provides.</p> <div class="section"> <h3 class="title-3">Code Index Menus</h3> <p>A quick way to navigate code in the current Python file is to use the source index menus shown at the top of the editor:</p> <img src="https://wingware.com/images/blog/code-navigation/index-menus.png" alt="/images/blog/code-navigation/index-menus.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="713px" /><p>Depending on the location of the caret in the editor, Wing may show multiple menus, as in the above example: One for the contents of the top level of the file, one for the contents of the current top-level scope, and additional menus for each sub-scope. Clicking on any of these provides an index and selecting an item jumps to that place in the source code.</p> </div> <div class="section"> <h3 class="title-3">Source Browser</h3> <p>In Wing Pro only, the <tt class="literal"><span class="pre">Source</span> <span class="pre">Browser</span></tt> in the <tt class="literal"><span class="pre">Tools</span></tt> menu provides another way to view an index of your source code, either for the current module, all project modules, or all classes:</p> <img src="https://wingware.com/images/blog/code-navigation/source-browser.png" alt="/images/blog/code-navigation/source-browser.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="357px" /><p>The scope being browsed and the types of symbols shown may be selected by clicking on the menus at the top of the tool. Double-clicking on items displays them in the editor.</p> <p>Browsing all project modules or classes assume that you have used <tt class="literal"><span class="pre">Add</span> <span class="pre">Existing</span> <span class="pre">Directory</span></tt> in the <tt class="literal"><span class="pre">Project</span></tt> menu to add your source code to your project. Typically the project should contain the code you are actively working on. Packages that your code uses can be left out of the project, unless you anticipate often wanting to open or search files in them. Wing will still be able to find them through the Python Path configured by Python or in Wing's <tt class="literal"><span class="pre">Project</span> <span class="pre">Properties</span></tt>.</p> <br> <br><p>That's it for now! We'll be back next week to conclude this <a class="reference" href="/hints">Wing Tips</a> mini-series on navigating Python code with Wing.</p> <p>As always, please don't hesitate to email <a class="reference" href="mailto:support&#64;wingware.com">support&#64;wingware.com</a> if you run into problems or have any questions.</p> </div> https://wingware.com/blog/code-navigation-2Thu, 21 Nov 2019 01:00:00 GMTNavigating Python Code with Wing Pro 7 (part 1 of 3)https://wingware.com/blog/code-navigation-1<p>Wing Python IDE includes a boatload of features aimed at making it easier to navigate and understand the structure of Python code. Some of these allow for quick navigation between the definition and uses of a symbol. Others provide a convenient index into source code. And still others quickly find and open files or navigate to symbols matching a name fragment.</p> <p>In the this and the next two <a class="reference" href="/hints">Wing Tips</a>, we'll take a look at each of these in turn.</p> <div class="section"> <h3 class="title-3">Goto Definition</h3> <p>To get from any use of a symbol in Python code to its definition, use <tt class="literal"><span class="pre">Goto</span> <span class="pre">Selected</span> <span class="pre">Symbol</span> <span class="pre">Defn</span></tt> in the <tt class="literal"><span class="pre">Source</span></tt> menu. This jumps to the <tt class="literal"><span class="pre">def</span></tt>, <tt class="literal"><span class="pre">class</span></tt>, or the point at which a variable or attribute was first defined.</p> <p>Another way to do this is to right-click on the symbol in the editor and select <tt class="literal"><span class="pre">Goto</span> <span class="pre">Definition</span></tt> or <tt class="literal"><span class="pre">Goto</span> <span class="pre">Definition</span> <span class="pre">in</span> <span class="pre">Other</span> <span class="pre">Split</span></tt>:</p> <img src="https://wingware.com/images/blog/code-navigation/goto-definition.gif" alt="/images/blog/code-navigation/goto-definition.gif" backrefs="" class="doc-image" dupnames="" ids="" names="" width="702px" /><p>The menus also give the key bindings for the commands, or you can bind your own key to the command <tt class="literal"><span class="pre">goto-selected-symbol-defn</span></tt> with the <tt class="literal"><span class="pre">User</span> <span class="pre">Interface</span> <span class="pre">&gt;</span> <span class="pre">Keyboard</span> <span class="pre">&gt;</span> <span class="pre">Custom</span> <span class="pre">Key</span> <span class="pre">Bindings</span></tt> preference.</p> <p>In some cases, jumping to a definition successfully depends on resolving imported modules correctly using the Python Path configured by Python. In most cases you will not need to add to this configuration, but doing so is possible with <tt class="literal"><span class="pre">Project</span> <span class="pre">Properties</span></tt> from Wing's <tt class="literal"><span class="pre">Project</span></tt> menu.</p> </div> <div class="section"> <h3 class="title-3">Navigation History</h3> <p>For this and all the other code navigation options, the <img src="https://wingware.com/images/doc/en/icons/wingide-back.png" alt="history-back" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> button at the top left of the editor may be used to return to the previous file or focus. Or move forward again in your navigation history with the <img src="https://wingware.com/images/doc/en/icons/wingide-forward.png" alt="history-forward" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> button.</p> </div> <div class="section"> <h3 class="title-3">Find Uses</h3> <p>In Wing Pro only, <tt class="literal"><span class="pre">Find</span> <span class="pre">Points</span> <span class="pre">of</span> <span class="pre">Use</span></tt> in the <tt class="literal"><span class="pre">Source</span></tt> menu or the editor's right-click context menu finds all points of use of a symbol in Python code:</p> <img src="https://wingware.com/images/blog/code-navigation/find-uses.png" alt="/images/blog/code-navigation/find-uses.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="702px" /><p>This search distinguishes between different but like-named symbols and will cover all project files and other files Wing finds on the configured Python Path. The tool's <tt class="literal"><span class="pre">Options</span></tt> menu provides control over which files are searched and what types of matches are shown.</p> </div> <div class="section"> <h3 class="title-3">Search in Files</h3> <p>To find all occurrences of other strings in Python files or in project files of any type, use the <tt class="literal"><span class="pre">Search</span> <span class="pre">in</span> <span class="pre">Files</span></tt> tool from the <tt class="literal"><span class="pre">Tools</span></tt> menu with <tt class="literal"><span class="pre">Look</span> <span class="pre">in</span></tt> set to <tt class="literal"><span class="pre">Project</span> <span class="pre">Files</span></tt> and <tt class="literal"><span class="pre">Filter</span></tt> set to the narrowest filter that includes the files that you wish to search:</p> <img src="https://wingware.com/images/blog/code-navigation/search-in-files.png" alt="/images/blog/code-navigation/search-in-files.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="699px" /><p>This tool supports text matching, wildcard, and regular expression searching and automatically updates the search results as files change.</p> <p>Searching on <tt class="literal"><span class="pre">Project</span> <span class="pre">Files</span></tt> assumes that you have used <tt class="literal"><span class="pre">Add</span> <span class="pre">Existing</span> <span class="pre">Directory</span></tt> in the <tt class="literal"><span class="pre">Project</span></tt> menu to add your source code to your project. Typically the project should contain the code you are actively working on. Packages that your code uses can be left out of the project, unless you anticipate often wanting to search them with <tt class="literal"><span class="pre">Search</span> <span class="pre">in</span> <span class="pre">Files</span></tt>.</p> <br> <br><p>That's it for now! We'll be back next week to continue this <a class="reference" href="/hints">Wing Tips</a> mini-series on navigating Python code with Wing.</p> <p>As always, please don't hesitate to email <a class="reference" href="mailto:support&#64;wingware.com">support&#64;wingware.com</a> if you run into problems or have any questions.</p> </div> https://wingware.com/blog/code-navigation-1Thu, 14 Nov 2019 01:00:00 GMTEfficient Flask Web Development with Wing 7https://wingware.com/blog/flask<p>Wing can develop and debug Python code running under <a class="reference" href="https://palletsprojects.com/p/flask/">Flask</a>, a web framework that is quick to get started with and easy to extend as your web application grows.</p> <p>To create a new project, use <tt class="literal"><span class="pre">New</span> <span class="pre">Project</span></tt> in Wing's <tt class="literal"><span class="pre">Project</span></tt> menu and select the project type <tt class="literal"><span class="pre">Flask</span></tt>. If Flask is not installed into your default Python, you may also need to set <tt class="literal"><span class="pre">Python</span> <span class="pre">Executable</span></tt> to the full path of the <tt class="literal"><span class="pre">python</span></tt> or <tt class="literal"><span class="pre">python.exe</span></tt> you want to use. This is the value of <tt class="literal"><span class="pre">sys.executable</span></tt> (after <tt class="literal"><span class="pre">import</span> <span class="pre">sys</span></tt>) in the desired Python installation or virtualenv.</p> <p>Next, add your files to the project with <tt class="literal"><span class="pre">Add</span> <span class="pre">Existing</span> <span class="pre">Directory</span></tt> in the <tt class="literal"><span class="pre">Project</span></tt> menu.</p> <div class="section"> <h3 class="title-3">Debugging Flask in Wing</h3> <p>To debug Flask in Wing you need to turn off Flask's built-in debugger, so that Wing's debugger can take over reporting exceptions. This is done by setting the <tt class="literal"><span class="pre">debug</span></tt> attribute on the Flask application to <tt class="literal"><span class="pre">False</span></tt>:</p> <div class="python-highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">debug</span> <span class="o">=</span> <span class="bp">False</span> </pre></div> <p>Then use <tt class="literal"><span class="pre">Set</span> <span class="pre">Current</span> <span class="pre">as</span> <span class="pre">Main</span> <span class="pre">Entry</span> <span class="pre">Point</span></tt> in the <tt class="literal"><span class="pre">Debug</span></tt> menu to set your main entry point, so you can start debugging from the IDE even if the main entry point file is not visible in the editor.</p> <p>Once debug is started, you can load pages from a browser to reach breakpoints or exceptions in your code. Output from the Flask process is shown in Wing's <tt class="literal"><span class="pre">Debug</span> <span class="pre">I/O</span></tt> tool.</p> </div> <div class="section"> <h3 class="title-3">Example</h3> <p>Here's an example of a complete &quot;Hello World&quot; Flask application that can be debugged with Wing:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">os</span> <span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span> <span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span> <span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span> <span class="k">def</span> <span class="nf">hello</span><span class="p">():</span> <span class="k">return</span> <span class="s">&quot;&lt;h3&gt;Hello World!&lt;/h3&gt;&lt;p&gt;Your app is working.&lt;/p&gt;&quot;</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span> <span class="k">if</span> <span class="s">&#39;WINGDB_ACTIVE&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">:</span> <span class="n">app</span><span class="o">.</span><span class="n">debug</span> <span class="o">=</span> <span class="bp">False</span> <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> </pre></div> <p>To try it, start debugging it in Wing and use the URL printed to the <tt class="literal"><span class="pre">Debug</span> <span class="pre">I/O</span></tt> tool to load the page in a web browser. Setting a breakpoint on the <tt class="literal"><span class="pre">return</span></tt> statement will stop there whenever the page is reloaded in the browser.</p> </div> <div class="section"> <h3 class="title-3">Setting up Auto-Reload with Wing Pro</h3> <p>With the above configuration, you will need to restart Flask whenever you make a change to your code, either with <tt class="literal"><span class="pre">Restart</span> <span class="pre">Debugging</span></tt> in the <tt class="literal"><span class="pre">Debug</span></tt> menu or with the <img src="https://wingware.com/images/doc/en/icons/wingide-debug-restart.png" alt="restart" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> toolbar icon.</p> <p>If you have Wing Pro, you can avoid the need to restart Flask by telling it to auto-restart when code changes on disk, and configuring Wing to automatically debug the restarted process.</p> <p>Flask is configured by adding a keyword argument to your <tt class="literal"><span class="pre">app.run()</span></tt> line:</p> <div class="python-highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">use_reloader</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> <p>Wing is configured by enabling <tt class="literal"><span class="pre">Debug</span> <span class="pre">Child</span> <span class="pre">Processes</span></tt> under the <tt class="literal"><span class="pre">Debug/Execute</span></tt> tab in <tt class="literal"><span class="pre">Project</span> <span class="pre">Properties</span></tt>, from the <tt class="literal"><span class="pre">Project</span></tt> menu. This tells Wing Pro to debug also child processes created by Flask, including the reloader process.</p> <p>Now Flask will automatically restart on its own whenever you save an already-loaded source file to disk, and Wing will debug the restarted process. You can add additional files for Flask to watch as follows:</p> <div class="python-highlight"><pre><span class="n">watch_files</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;/path/to/file1&#39;</span><span class="p">,</span> <span class="s">&#39;/path/to/file2&#39;</span><span class="p">]</span> <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">use_reloader</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">extra_files</span><span class="o">=</span><span class="n">watch_files</span><span class="p">)</span> </pre></div> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> <p>As always, please don't hesitate to email <a class="reference" href="mailto:support&#64;wingware.com">support&#64;wingware.com</a> if you run into problems or have any questions.</p> </div> https://wingware.com/blog/flaskThu, 31 Oct 2019 01:00:00 GMTUsing Matplotlib with Wing 7https://wingware.com/blog/matplotlib<p>Wing supports interactive development and debugging of Python code designed for the Matplotlib numerical and scientific plotting library, so plots can be shown and updated from the command line. For example, two plots could be shown in succession by typing the following into Wing's <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt>, one line at a time:</p> <div class="python-highlight"><pre><span class="kn">from</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">import</span> <span class="n">plot</span><span class="p">,</span> <span class="n">show</span> <span class="n">x</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="n">show</span><span class="p">()</span> <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span> <span class="n">plot</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> </pre></div> <p>Wing sets up the environment so that <tt class="literal"><span class="pre">show()</span></tt> runs to completion and immediately returns you to the prompt, rather than waiting until the plot is closed. In addition, Wing calls Matplotlib's main loop to keep plots windows interactive and updating while you are at the prompt. This allows plots to be added or changed without restarting a process or interrupting your work flow:</p> <img src="https://wingware.com/images/blog/matplotlib/interactive-shell.gif" alt="/images/blog/matplotlib/interactive-shell.gif" backrefs="" caption="Working interactively with Matplotlib from Wing's Python Shell" class="doc-image" dupnames="" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" /><p style="padding-top:0px;"><i>Working interactively with Matplotlib from Wing's Python Shell</i></p><p><strong>Evaluating Files and Selections</strong></p> <p>Code from the editor can be executed in the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> with the <tt class="literal"><span class="pre">Evaluate</span> <span class="pre">...</span> <span class="pre">in</span> <span class="pre">Python</span> <span class="pre">Shell</span></tt> items in the <tt class="literal"><span class="pre">Source</span></tt> menu and in the editor's right-click context menu. By default the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> restarts automatically before evaluating a whole file, but this can be disabled in its <tt class="literal"><span class="pre">Options</span></tt> menu.</p> <p><strong>Active Ranges</strong></p> <p>Wing also allows you to set a selected range of lines in the editor as the &quot;active range&quot; for the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> by clicking the <img src="https://wingware.com/images/doc/en/icons/wingide-active-range-set.png" alt="setactive" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> icon in the top right of the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> tool:</p> <img src="https://wingware.com/images/blog/matplotlib/active-range.png" alt="/images/blog/matplotlib/active-range.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="570px" /><p>Wing highlights and maintains the active range as you edit it in the editor, and it can be re-evaluated easily with the <img src="https://wingware.com/images/doc/en/icons/wingide-execute.png" alt="runactive" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> icon that appears in the top right of the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> once an active range has been set into it.</p> <p>Use the <img src="https://wingware.com/images/doc/en/icons/wingide-active-range-clear.png" alt="clearactive" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> icon to clear the active range from the editor and shell.</p> <p><strong>Supported Backends</strong></p> <p>Interactive development is supported for the <tt class="literal"><span class="pre">TkAgg</span></tt>, <tt class="literal"><span class="pre">GTKAgg</span></tt>, <tt class="literal"><span class="pre">GtkCairo</span></tt>, <tt class="literal"><span class="pre">WXAgg</span></tt> (for wxPython 2.5+), <tt class="literal"><span class="pre">Qt5Agg</span></tt>, <tt class="literal"><span class="pre">Qt4Agg</span></tt>, <tt class="literal"><span class="pre">MacOSX</span></tt>, and <tt class="literal"><span class="pre">WebAgg</span></tt> backends. It will not work with other backends.</p> <div class="section"> <h3 class="title-3">Debugging</h3> <p>Code can be debugged either by launching a file with <img src="https://wingware.com/images/doc/en/icons/wingide-debug-continue.png" alt="startdebug" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> in the toolbar (or <tt class="literal"><span class="pre">Start/Continue</span></tt> the <tt class="literal"><span class="pre">Debug</span></tt> menu) or by enabling debug in the integrated <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> and working from there. In either case, Wing can be used to reach breakpoints or exceptions, step through code, and view the program's data. For general information on using Wing's debugger see the <a class="reference" href="/debug/quick-start">Debugger Quick Start</a>.</p> <p>When executing code that includes <tt class="literal"><span class="pre">show()</span></tt> in the debugger, Wing will block within the <tt class="literal"><span class="pre">show()</span></tt> call just as Python would if launched on the same file. This is by design, since the debugger seeks to replicate as closely as possible how Python normally runs.</p> <p>However, interactive development from a breakpoint or exception is still possible, as described below. This capability can be used to load setup code before interacting with Matplotlib, or to try out a fix when an exception has been reached.</p> <p><strong>Interactive Debugging from the Debug Console (Wing Pro only)</strong></p> <p>Whenever the debugger is stopped at a breakpoint or exception, Wing Pro's <tt class="literal"><span class="pre">Debug</span> <span class="pre">Console</span></tt> provides a command prompt that may be used to inspect and interact with the paused debug process. Commands entered here run in the context of the currently selected debug stack frame:</p> <img src="https://wingware.com/images/blog/matplotlib/debug-console.png" alt="/images/blog/matplotlib/debug-console.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="673px" /><p>The tool implements the same support for interactive development provided by the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt>, so plots may be shown and modified interactively whenever Wing's debugger is paused. Once the debug process is continued, Wing switches off interactive mode and returns to behaving in the same way that Python would when running the code outside of the debugger.</p> <div class="note"> Interactive development from the <tt class="literal"><span class="pre">Debug</span> <span class="pre">Console</span></tt> requires that you have already imported <tt class="literal"><span class="pre">matplotlib</span></tt> in the code that you are debugging or in a previous command entered in the console. Otherwise <tt class="literal"><span class="pre">show()</span></tt> may block and plots won't be updated.</div> <p><strong>Interactive Debugging from the Python Shell</strong></p> <p>Another way to combine the debugger with interactive development is to turn on both <tt class="literal"><span class="pre">Enable</span> <span class="pre">Debugging</span></tt> and <tt class="literal"><span class="pre">Enable</span> <span class="pre">Recursive</span> <span class="pre">Prompt</span></tt> in the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt>'s <tt class="literal"><span class="pre">Options</span></tt> menu:</p> <img src="https://wingware.com/images/blog/matplotlib/shell-options.png" alt="/images/blog/matplotlib/shell-options.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="305px" /><p>This causes Wing to add a breakpoint margin to the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> and to stop in the debugger if an exception or breakpoint is reached, either in code in the editor or code that was entered into the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt>.</p> <p>The option <tt class="literal"><span class="pre">Enable</span> <span class="pre">Recursive</span> <span class="pre">Prompt</span></tt> causes Wing to show a new recursive prompt in the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt> whenever the debugger is paused, rather than waiting for completion of the original command before showing another prompt. Showing or updating plots from recursive prompts works interactively in the same way as described earlier.</p> <p>If another exception or breakpoint is reached, Wing stops at those as well, recursively to any depth. Continuing the debug process from a recursive prompt completes the innermost invocation and returns to the previous recursive prompt, unless another exception or breakpoint is reached first.</p> </div> <div class="section"> <h3 class="title-3">Trouble-shooting</h3> <p>If <tt class="literal"><span class="pre">show()</span></tt> blocks when typed into the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt>, if plots fail to update, or if you run into other event loop problems while working with Matplotlib, then the following may help solve the problem:</p> <p>(1) When working in the <tt class="literal"><span class="pre">Debug</span> <span class="pre">Console</span></tt>, evaluate the imports that set up Matplotlib first, so that Wing can initialize its event loop support before <tt class="literal"><span class="pre">show()</span></tt> is called. Evaluating a whole file at once in the <tt class="literal"><span class="pre">Debug</span> <span class="pre">Console</span></tt> (but not the <tt class="literal"><span class="pre">Python</span> <span class="pre">Shell</span></tt>) will cause <tt class="literal"><span class="pre">show()</span></tt> to block if Matplotlib was not previously imported.</p> <p>(2) In case there is a problem with the specific Matplotlib backend that you are using, try the following as a way to switch to another backend before issuing any other commands:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib</span> <span class="n">matplotlib</span><span class="o">.</span><span class="n">use</span><span class="p">(</span><span class="s">&#39;TkAgg&#39;</span><span class="p">)</span> </pre></div> <p>Instead of <tt class="literal"><span class="pre">TkAgg</span></tt> you may also try other supported backends, including <tt class="literal"><span class="pre">Qt5Agg</span></tt> (which requires that Qt5 is installed) or <tt class="literal"><span class="pre">WebAgg</span></tt> (which uses a web browser for plot display).</p> <p>As always, please email <a class="reference" href="mailto:support&#64;wingware.com">support&#64;wingware.com</a> if you run into problems that you cannot resolve.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/matplotlibThu, 17 Oct 2019 01:00:00 GMTPython Code Warnings in Wing Pro 7https://wingware.com/blog/code-warnings<p>Wing Pro 7 introduced an improved code warnings system that flags likely errors as you work on Python code, using both Wing's built-in static analysis system and (optionally) external code checkers like Pylint, pep8, and mypy. Likely problems are indicated on the editor and listed in the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool:</p> <img src="https://wingware.com/images/blog/code-warnings/overview.png" alt="/images/blog/code-warnings/overview.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="893px" /><p>Examples of warnings that Wing might flag include syntax errors, indentation problems, uses of an undefined variable, imports that cannot be resolved, or variables that are set but never used.</p> <p>Code warnings save development time because they help to identify errors before code is even run. New code is checked as you work, although Wing will wait until you have finished typing so that it doesn't warn about code that is still being entered.</p> <div class="section"> <h3 class="title-3">Navigating Warnings</h3> <p>Clicking on warnings in the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool or pressing the <tt class="literal"><span class="pre">Enter</span></tt> key in the list navigates to that warning in the editor, highlighting it briefly with a callout, as configured from the <tt class="literal"><span class="pre">Editor</span> <span class="pre">&gt;</span> <span class="pre">Callouts</span></tt> preferences group.</p> <p>The <img src="https://wingware.com/images/doc/en/icons/wingide-warnings.png" alt="warning" backrefs="" class="inline-image" dupnames="" height="18" ids="" names="" width="18" /> code warnings icon appears in the top right of any editor that has some code warnings. This can be clicked in order to jump to selected warnings in the file.</p> <p>When code warnings are displayed on the editor, hovering the mouse cursor over the indicator will display details for that warning in a tooltip, as shown above.</p> </div> <div class="section"> <h3 class="title-3">Configuration</h3> <p>The types of code warnings that Wing shows can be configured from the <tt class="literal"><span class="pre">Configuration:</span> <span class="pre">Defaults</span></tt> page in the drop-down menu at the top of the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool. All the warnings Wing supports are documented in <a class="reference" href="/doc/warnings/warning-types">Warning Types</a>. Some of these offer configuration options to control which variants of that type of warning will be shown:</p> <img src="https://wingware.com/images/blog/code-warnings/config.png" alt="/images/blog/code-warnings/config.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="417px" /><p>Incorporating warnings found by external checkers like Pylint, pep8, and mypy is also done from this configuration page, as described previously in <a class="reference" href="/hints/external-checkers">Using External Code Quality Checkers with Wing Pro 7</a></p> </div> <div class="section"> <h3 class="title-3">Disabling Warnings</h3> <p>Since all code checkers have only a limited understanding of what happens when code is actually run, they may show incorrect warnings. Wing allows you to disable warnings either for a single case, for an entire file, or for all files.</p> <p>The quickest way to disable a warning is to press the red <tt class="literal"><span class="pre">X</span></tt> icon that appears in the tooltips shown in the editor or in the currently selected item in the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool:</p> <img src="https://wingware.com/images/blog/code-warnings/x-icon.png" alt="/images/blog/code-warnings/x-icon.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="333px" /><p>Wing disables most individual warnings only for the scope it appears in, so an identical problem in another scope will still be flagged. However, undefined attribute warnings are always disabled in all files where that attribute appears.</p> <p><strong>How it Works</strong></p> <p>When a warning is disabled, Wing adds a rule to the <tt class="literal"><span class="pre">Configuration:</span> <span class="pre">Disabled</span> <span class="pre">Warnings</span></tt> page in the drop-down menu at the top of the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool:</p> <img src="https://wingware.com/images/blog/code-warnings/disabled.png" alt="/images/blog/code-warnings/disabled.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="396px" /><p>Rules can be removed from here to reenable a warning, dragged between sections to control how widely the warning is being ignored, or edited by right-clicking in order to fine-tune the scope and nature of the rule.</p> <p>For external checkers like Pylint, pep8, and mypy, warnings are disabled globally by the type of warning, making it relatively easy to develop a custom filter according to coding style and individual preference.</p> <p><strong>Resolving Imports</strong></p> <p>One thing to note here is that all the error checkers need some knowledge of your <tt class="literal"><span class="pre">PYTHONPATH</span></tt> to trace down imports correctly. Often, no additional configuration is needed, but in cases where imports cannot be resolved you may need to add to the <tt class="literal"><span class="pre">Python</span> <span class="pre">Path</span></tt> in Wing's <tt class="literal"><span class="pre">Project</span> <span class="pre">Properties</span></tt>, from the <tt class="literal"><span class="pre">Project</span></tt> menu.</p> </div> <div class="section"> <h3 class="title-3">Sharing Code Warnings Configurations</h3> <p>The current code warnings configuration, including all the warnings you have disabled, may be exported to the user settings area, or to a selected file from the <tt class="literal"><span class="pre">Options</span></tt> menu in the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool. Projects may then share the configuration through the <tt class="literal"><span class="pre">Use</span> <span class="pre">Configuration</span> <span class="pre">From</span></tt> item in the <tt class="literal"><span class="pre">Code</span> <span class="pre">Warnings</span></tt> tool's <tt class="literal"><span class="pre">Options</span></tt> menu:</p> <img src="https://wingware.com/images/blog/code-warnings/options-menu.png" alt="/images/blog/code-warnings/options-menu.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="416px" /><p>The shared configuration file may be checked into revision control, so it can also be used by other developers working on your project.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/code-warningsThu, 10 Oct 2019 01:00:00 GMTDebugging Python Code Running in Docker Containers with Wing 7https://wingware.com/blog/docker<p><a class="reference" href="https://www.docker.com/">Docker</a> is a containerization system that uses a relatively light-weight form of virtualization to package and isolate application components from the host system, making it easier to spin up uniformly configured virtual machines for use in application development, testing, and deployment.</p> <p>Wing 7 can be used to develop and debug Python code running inside of Docker containers. This is accomplished by setting up a mapping of local (host-side) directories into the container, and then configuring Wing so it can accept debug connections from the container.</p> <div class="section"> <h3 class="title-3">Prerequisites</h3> <p>Before you can work with Docker you will need to download and install it.</p> <p><strong>On Windows and macOS</strong>, downloading Docker Desktop from the <a class="reference" href="https://www.docker.com/">Docker</a> website is the easiest way to install it. Be sure to launch the Docker Desktop after you install it, so the daemon is started.</p> <p><strong>On most Linux distributions</strong>, Docker CE (the free community edition) can be installed with the <tt class="literal"><span class="pre">docker-engine</span></tt> package as <a class="reference" href="https://runnable.com/docker/install-docker-on-linux">described here</a>.</p> <p>You should also install <a class="reference" href="https://wingware.com/downloads/wing-pro">Wing Pro</a> if you don't already have it.</p> </div> <div class="section"> <h3 class="title-3">Create a Working Example</h3> <p>Next set up a small real world example by creating a directory <tt class="literal"><span class="pre">docker</span></tt> and placing the following files into it.</p> <p><strong>Dockerfile</strong>:</p> <pre class="literal-block"> FROM python:3.7 WORKDIR /app RUN pip install --trusted-host pypi.python.org Flask EXPOSE 80 CMD [&quot;python&quot;, &quot;app.py&quot;] </pre> <p><strong>app.py:</strong></p> <div class="python-highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span> <span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span> <span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span> <span class="k">def</span> <span class="nf">hello</span><span class="p">():</span> <span class="k">return</span> <span class="s">&quot;&lt;h3&gt;Hello World!&lt;/h3&gt;Your app is working.&lt;br/&gt;&lt;/br/&gt;&quot;</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span> <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s">&#39;0.0.0.0&#39;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">use_reloader</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> <p>Then build the Docker container by typing the following in the <tt class="literal"><span class="pre">docker</span></tt> directory:</p> <pre class="literal-block"> docker build --tag=myapp . </pre> <p>You can now run your container like this:</p> <pre class="literal-block"> docker run -v &quot;/path/to/docker&quot;:/app -p 4000:80 myapp </pre> <p>You will need to substitute <tt class="literal"><span class="pre">/path/to/docker</span></tt> with the path to the <tt class="literal"><span class="pre">docker</span></tt> directory you created above; the quotes make it work if the path has spaces in it.</p> <p>You can now try this tiny Flask- web app by pointing a browser running on your host system at it:</p> <p><strong>If you are using Docker Desktop</strong>, then use <a class="reference" href="http://localhost:4000/">http://localhost:4000/</a></p> <p><strong>If you are using Docker CE</strong>, you will need to determine the IP address of your container and use that instead of <tt class="literal"><span class="pre">localhost</span></tt>. One way to do this is to type <tt class="literal"><span class="pre">docker</span> <span class="pre">ps</span></tt> to find the Container ID for your container and then use it in the following in place of <tt class="literal"><span class="pre">c052478b0f8a</span></tt>:</p> <pre class="literal-block"> docker inspect -f &quot;{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}&quot; c052478b0f8a </pre> <p>Notice that if you make a change to <tt class="literal"><span class="pre">app.py</span></tt> in Wing, then the change will be reflected in your browser when you reload the page. This is due to using both the <tt class="literal"><span class="pre">-v</span></tt> argument for <tt class="literal"><span class="pre">docker</span> <span class="pre">run</span></tt> to mount a volume in the container, and the fact that <tt class="literal"><span class="pre">app.run()</span></tt> for Flask is being passed <tt class="literal"><span class="pre">use_reloader=True</span></tt>.</p> </div> <div class="section"> <h3 class="title-3">Configure Debugging</h3> <p>In order to debug <tt class="literal"><span class="pre">app.py</span></tt> in Wing, you will need to copy in and configure some additional files that allow invocation of Wing's debugger and connection to the IDE.</p> <p><strong>(1) Install the debugger</strong></p> <p>To access Wing's debugger on the container, add another <tt class="literal"><span class="pre">-v</span></tt> mapping to your <tt class="literal"><span class="pre">docker</span> <span class="pre">run</span></tt> command line, so the Wing installation on the host is made available to the container. For example on Windows:</p> <pre class="literal-block"> docker run -v &quot;C:/Program Files (x86)/Wing Pro 7.1&quot;:/wingpro7 -v C:/Users/test/docker:/app myapp </pre> <p>Or on Linux:</p> <pre class="literal-block"> docker run -v /usr/lib/wingpro7:/wingpro7 -v /home/test/docker:/app myapp </pre> <p>Or for macOS:</p> <pre class="literal-block"> docker run -v /Applications/WingPro.app/Contents/Resources:/wingpro7 -v /Users/test/docker:/app myapp </pre> <p>You will need to substitute the correct installation location for Wing on your host, which can be seen in Wing's <tt class="literal"><span class="pre">About</span></tt> box, and the full path to the <tt class="literal"><span class="pre">docker</span></tt> directory you created earlier.</p> <p>Mapping the Wing installation across OSes (for example from Windows host to Linux container) works because Wing's installation has all the files necessary files for debugging on every supported OS.</p> <p><strong>(2) Copy and configure wingdbstub.py</strong></p> <p>Debugging is initiated on the Docker side by importing Wing's <tt class="literal"><span class="pre">wingdbstub</span></tt> module. To use this, copy <tt class="literal"><span class="pre">wingdbstub.py</span></tt> from your Wing installation to your mapped directory on the host. For example on a Windows host:</p> <pre class="literal-block"> copy &quot;C:/Program Files (x86)/Wing Pro 7.1/wingdbstub.py&quot; C:/Users/test/docker </pre> <p>Or on a Linux host:</p> <pre class="literal-block"> cp /usr/lib/wingpro7/wingdbstub.py /home/test/docker </pre> <p>Or a macOS host:</p> <pre class="literal-block"> cp /Applications/WingPro.app/Contents/Resources/wingdbstub.py /Users/test/docker </pre> <p>After copying, you will need to edit the file to change <tt class="literal"><span class="pre">kWingHostPost</span></tt> from <tt class="literal"><span class="pre">localhost:50005</span></tt> to a value that uses the IP address or name of the host computer, for example if your host's IP address is <tt class="literal"><span class="pre">192.168.1.50</span></tt>:</p> <div class="python-highlight"><pre><span class="n">kWingHostPort</span> <span class="o">=</span> <span class="s">&#39;192.168.1.50:50005&#39;</span> </pre></div> <p>You will also need to set <tt class="literal"><span class="pre">WINGHOME</span></tt> to the location where you have mapped your Wing installation on the container:</p> <div class="python-highlight"><pre><span class="n">WINGHOME</span> <span class="o">=</span> <span class="s">&#39;/wingpro7&#39;</span> </pre></div> <p><strong>(3) Enable access</strong></p> <p>Next you need to copy the authentication token file <tt class="literal"><span class="pre">wingdebugpw</span></tt> from the <tt class="literal"><span class="pre">Settings</span> <span class="pre">Directory</span></tt> listed in Wing's <tt class="literal"><span class="pre">About</span></tt> box to the same directory as your copy of <tt class="literal"><span class="pre">wingdbstub.py</span></tt>, in this case the <tt class="literal"><span class="pre">docker</span></tt> directory on the host system.</p> <p>Then add to the <tt class="literal"><span class="pre">Debugger</span> <span class="pre">&gt;</span> <span class="pre">Advanced</span> <span class="pre">&gt;</span> <span class="pre">Allowed</span> <span class="pre">Hosts</span></tt> preference either the host's IP address (if using Docker Desktop) or the container's IP address determined with <tt class="literal"><span class="pre">docker</span> <span class="pre">inspect</span></tt> as described above (if using Docker CE). The host IP is used for Docker Desktop because of how it configures networking for containers; there is access from container to host but no access in the other direction, so the host thinks it is receiving a connection from one of its own network interfaces.</p> <p>You will also need to tell Wing to listen for debug connections initiated from the outside by clicking on the bug icon in the lower left of Wing's window and enabling <tt class="literal"><span class="pre">Accept</span> <span class="pre">Debug</span> <span class="pre">Connections</span></tt>.</p> <p><strong>(4) Establish a file mapping</strong></p> <p>If the <tt class="literal"><span class="pre">docker</span></tt> directory you mapped with the <tt class="literal"><span class="pre">-v</span></tt> option for <tt class="literal"><span class="pre">docker</span> <span class="pre">run</span></tt> does not appear on the same path on the host and container then you will need to communicate the mapping to Wing as well, with the <tt class="literal"><span class="pre">Debugger</span> <span class="pre">&gt;</span> <span class="pre">Advanced</span> <span class="pre">&gt;</span> <span class="pre">Location</span> <span class="pre">Map</span></tt> preference.</p> <p>For the <tt class="literal"><span class="pre">docker</span> <span class="pre">run</span></tt> example above and container IP address of <tt class="literal"><span class="pre">172.17.0.2</span></tt> you would add an entry as follows:</p> <b>Remote IP Address:</b> 172.17.0.2<br/> <b>File Mappings:</b><br/> &nbsp;&nbsp;&nbsp;&nbsp;&#x1f518;&nbsp;<b>Specify Mapping</b><br/> &nbsp;&nbsp;&nbsp;&nbsp;<b>Remote:</b> /app<br/> &nbsp;&nbsp;&nbsp;&nbsp;<b>Local:</b> C:/Users/test/docker<br/> <br><p>This step could be skipped entirely if the location of files on the container and the host are the same (for example using <tt class="literal"><span class="pre">/app</span></tt> also on the host instead of creating a directory named <tt class="literal"><span class="pre">docker</span></tt>).</p> <p>Also, if using Docker Desktop where the container IP is the same as the hosts's, it is important to choose a location for the container side of the mapping that either (a) does not exist on the host, or (b) is the same as the location on the host. If the directory exists on the host but has different Python files in it, the <tt class="literal"><span class="pre">Location</span> <span class="pre">Map</span></tt> will be incorrectly applied to them if you try to debug them.</p> <p><strong>(5) Initiate debug</strong></p> <p>Once these steps are complete, you can initiate debug from Python code running in the container by importing the module <tt class="literal"><span class="pre">wingdbstub.py</span></tt> as follows:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">wingdbstub</span> </pre></div> <p>This can be added as the first line of <tt class="literal"><span class="pre">app.py</span></tt>. After saving the file, Flask should auto-reload it, which will initiate debug and connect to the IDE so that the bug icon in the lower left of Wing's Window turns green and the toolbar changes to its debug configuration. The application keeps running until it reaches a breakpoint or exception.</p> <p>Next set a breakpoint by clicking leftmost margin to the left of the <tt class="literal"><span class="pre">return</span></tt> statement in <tt class="literal"><span class="pre">app.py</span></tt> and then trigger it by reloading the page in your browser. Now you can use Wing to step through and inspect the data being used in the debug process.</p> <p>To learn more about Wing's debugger, take a look at the <tt class="literal"><span class="pre">Tutorial</span></tt> in Wing's Help menu or the <tt class="literal"><span class="pre">Debugging</span> <span class="pre">Code</span></tt> section of the <a class="reference" href="/doc/howtos/quickstart">Quick Start Guide</a>.</p> </div> <div class="section"> <h3 class="title-3">Trouble-Shooting</h3> <p>If your configuration does not work, try setting <tt class="literal"><span class="pre">kLogFile</span></tt> in your copy of <tt class="literal"><span class="pre">wingdbstub.py</span></tt> to see whether the debugger is reporting errors. Also, looking at the end of <tt class="literal"><span class="pre">ide.log</span></tt> in the <tt class="literal"><span class="pre">Settings</span> <span class="pre">Directory</span></tt> listed in Wing's <tt class="literal"><span class="pre">About</span></tt> box may reveal why a connection is failing, if it is being refused by the IDE.</p> <p>Setting <tt class="literal"><span class="pre">kExitOnFailure</span></tt> in your copy of <tt class="literal"><span class="pre">wingdbstub.py</span></tt> is another way to see why debug or the connection to the IDE is failing. In this case, when you restart the container it will fail to start and print a message indicating the error encountered during <tt class="literal"><span class="pre">import</span> <span class="pre">wingdbstub</span></tt>.</p> <p>If the debug connection is established but breakpoints are not reached, the <tt class="literal"><span class="pre">Location</span> <span class="pre">Map</span></tt> preference is likely incorrect. One way to diagnose this is to add <tt class="literal"><span class="pre">assert</span> <span class="pre">0</span></tt> to your code. Wing will always stop on that and will report the file it thinks it should be opening in the <tt class="literal"><span class="pre">Exceptions</span></tt> tool.</p> <p>And, as always, don't hesitate to email <a class="reference" href="mailto:support&#64;wingware.com">support&#64;wingware.com</a> for help.</p> </div> <div class="section"> <h3 class="title-3">Notes</h3> <p>Docker CE (but not Docker Desktop) is sometimes used to host a more complete installation of Linux, acting more like a stand-alone system that includes the ability to <tt class="literal"><span class="pre">ssh</span></tt> from the host system into the container. In this case, Wing Pro's <a class="reference" href="https://wingware.com/doc/proj/remote-hosts">Remote Development</a> capability can be used, with much less manual configuration, to debug code running under Docker. For more information, see <a class="reference" href="/doc/howtos/remote-development">Remote Python Development</a> (if the debug process can be launched from the IDE) or <a class="reference" href="/doc/howtos/debugging-web-remote">Remote Web Development</a> (if the debug process is launched from outside of the IDE).</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/dockerTue, 24 Sep 2019 01:00:00 GMTViewing Arrays and Data Frames in Wing Pro 7https://wingware.com/blog/array-viewer<p>Wing Pro 7 introduced an array and data frame viewer that can be used to inspect data objects in the debugger. Values are transferred to the IDE according to what portion of the data is visible on the screen, so working with large data sets won't slow down the IDE.</p> <p>The array viewer works with Pandas, numpy, sqlite3, xarray, Python's builtin lists, tuples, and dicts, and other classes that emulate lists, tuples, or dicts.</p> <p>To use the array viewer, right-click on a value in the <tt class="literal"><span class="pre">Stack</span> <span class="pre">Data</span></tt> tool in Wing Pro and select <tt class="literal"><span class="pre">Show</span> <span class="pre">Value</span> <span class="pre">as</span> <span class="pre">Array</span></tt>:</p> <img src="https://wingware.com/images/blog/array-viewer/menu.png" alt="/images/blog/array-viewer/menu.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="1018px" /><p>This reveals the array viewer and displays the selected item from the <tt class="literal"><span class="pre">Stack</span> <span class="pre">Data</span></tt> tree, in this case the global variable <tt class="literal"><span class="pre">pandas_df</span></tt>:</p> <img src="https://wingware.com/images/blog/array-viewer/array.png" alt="/images/blog/array-viewer/array.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="1019px" /><p>Wing fetches data for display as you move the scroll bars. The <tt class="literal"><span class="pre">Filter</span></tt> can be used to display only matching rows:</p> <img src="https://wingware.com/images/blog/array-viewer/filter.png" alt="/images/blog/array-viewer/filter.png" backrefs="" class="doc-image" dupnames="" ids="" names="" width="582px" /><p>The drop down next to the <tt class="literal"><span class="pre">Filter</span></tt> field may be used to select plain text, wildcard, or regular expression searching, to control whether searches are case sensitive, and to select whether to search on all columns or only the visible columns.</p> <p>If more space is needed to view data, the <tt class="literal"><span class="pre">Stack</span> <span class="pre">Data</span></tt> tool's tab can be dragged out of the window, to create a separate window for it.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> https://wingware.com/blog/array-viewerThu, 19 Sep 2019 01:00:00 GMTPresentation Mode in Wing 7https://wingware.com/blog/presentation-mode<p>Presentation Mode, added in Wing 7, temporarily applies a selected magnification to the entire user interface, so the screen can be read more easily during meetings or talks.</p> <p>To activate this mode, check on <tt class="literal"><span class="pre">Presentation</span> <span class="pre">Mode</span></tt> in the high-level configuration menu accessed with the <img src="https://wingware.com/images/blog/dark-mode/menu.png" alt="menuicon" backrefs="" class="inline-image" dupnames="" ids="" names="" /> icon at the top right of Wing's window:</p> <img src="https://wingware.com/images/blog/presentation-mode/menu.png" alt="/images/blog/presentation-mode/menu.png" backrefs="" class="doc-image" dupnames="" height="320px" ids="" names="" width="275px" /><p>You will be presented with a confirmation dialog that also provides a link to the preference that controls the level of magnification:</p> <img src="https://wingware.com/images/blog/presentation-mode/dialog.png" alt="/images/blog/presentation-mode/dialog.png" backrefs="" class="doc-image" dupnames="" height="118px" ids="" names="" width="672px" /><p>To apply the mode change, Wing restarts and reloads the current project in the same state as you left it, but with the contents of the window magnified.</p> <div class="section"> <h3 class="title-3">Before</h3> <img src="https://wingware.com/images/blog/presentation-mode/window-1.0.png" alt="/images/blog/presentation-mode/window-1.0.png" backrefs="" class="doc-image" dupnames="" ids="" names="" /></div> <div class="section"> <h3 class="title-3">After</h3> <img src="https://wingware.com/images/blog/presentation-mode/window-1.5.png" alt="/images/blog/presentation-mode/window-1.5.png" backrefs="" class="doc-image" dupnames="" ids="" names="" /><p>To disable Presentation Mode, uncheck the high-level configuration menu item again.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/presentation-modeWed, 11 Sep 2019 01:00:00 GMTDark Mode and Color Configuration in Wing Python IDEhttps://wingware.com/blog/dark-mode<p>Wing 7 added four new dark color palettes and the ability to quickly toggle between light and dark mode using the <img src="https://wingware.com/images/blog/dark-mode/menu.png" alt="menuicon" backrefs="" class="inline-image" dupnames="" ids="" names="" /> menu icon in the top right of the IDE window. When <tt class="literal"><span class="pre">Dark</span> <span class="pre">Mode</span></tt> is selected, Wing switches to the most recently used dark color configuration, or the default dark configuration if none has been used.</p> <p>To select which dark mode is used, change <tt class="literal"><span class="pre">Color</span> <span class="pre">Palette</span></tt> on the first page of Wing's <tt class="literal"><span class="pre">Preferences</span></tt>. The dark palettes that ship with Wing 7 are:</p> <img src="https://wingware.com/images/blog/dark-mode/black-background.png" alt="/images/blog/dark-mode/black-background.png" backrefs="" caption="Black Background: The classic original dark mode for Wing" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Black Background: The classic original dark mode for Wing</i></p><img src="https://wingware.com/images/blog/dark-mode/cherry-blossom.png" alt="/images/blog/dark-mode/cherry-blossom.png" backrefs="" caption="Cherry Blossom: New in Wing 7" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Cherry Blossom: New in Wing 7</i></p><img src="https://wingware.com/images/blog/dark-mode/dracula.png" alt="/images/blog/dark-mode/dracula.png" backrefs="" caption="Dracula: New in Wing 7" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Dracula: New in Wing 7</i></p><img src="https://wingware.com/images/blog/dark-mode/monokai.png" alt="/images/blog/dark-mode/monokai.png" backrefs="" caption="Monokai" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Monokai</i></p><img src="https://wingware.com/images/blog/dark-mode/one-dark.png" alt="/images/blog/dark-mode/one-dark.png" backrefs="" caption="One Dark: The default dark color palette" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>One Dark: The default dark color palette</i></p><img src="https://wingware.com/images/blog/dark-mode/positronic.png" alt="/images/blog/dark-mode/positronic.png" backrefs="" caption="Positronic: New in Wing 7" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Positronic: New in Wing 7</i></p><img src="https://wingware.com/images/blog/dark-mode/solarized-dark.png" alt="/images/blog/dark-mode/solarized-dark.png" backrefs="" caption="Solarized Dark" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Solarized Dark</i></p><img src="https://wingware.com/images/blog/dark-mode/sun-steel.png" alt="/images/blog/dark-mode/sun-steel.png" backrefs="" caption="Sun Steel: New in Wing 7" class="doc-image" dupnames="" height="574px" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" width="773px" /><p style="padding-top:0px;"><i>Sun Steel: New in Wing 7</i></p><p>In most cases you will also want to enable the <tt class="literal"><span class="pre">User</span> <span class="pre">Interface</span> <span class="pre">&gt;</span> <span class="pre">Use</span> <span class="pre">Color</span> <span class="pre">Palette</span> <span class="pre">Throughout</span> <span class="pre">the</span> <span class="pre">UI</span></tt> preference, so that the color palette is applied to more than just editors. This preference is enabled automatically when the <tt class="literal"><span class="pre">Dark</span> <span class="pre">Mode</span></tt> menu item is used for the first time, and is enabled in all of the above screenshots. However, it may be disabled so only the editors are displayed dark. Wing will remember that choice when subsequently changing between light and dark modes.</p> <div class="note"> Note that on macOS 10.14+ with Wing 7.1+, the system-defined Dark Mode may be used instead, by leaving the <tt class="literal"><span class="pre">User</span> <span class="pre">Interface</span> <span class="pre">&gt;</span> <span class="pre">Use</span> <span class="pre">Color</span> <span class="pre">Palette</span> <span class="pre">Throughout</span> <span class="pre">the</span> <span class="pre">UI</span></tt> preference unchecked, and then selecting Dark Mode in the macOS System Preferences. In this approach, the <tt class="literal"><span class="pre">Color</span> <span class="pre">Palette</span></tt> preference in Wing should be set to <tt class="literal"><span class="pre">Classic</span> <span class="pre">Default</span></tt> or one of the dark color palettes.</div> <div class="section"> <h3 class="title-3">Color Configuration</h3> <p>Aside from selecting the overall color palette with the <tt class="literal"><span class="pre">User</span> <span class="pre">Interface</span> <span class="pre">&gt;</span> <span class="pre">Color</span> <span class="pre">Palette</span></tt> preference, it is also possible to override individual colors throughout the preferences, or to write your own color palette, including colors for the UI and optionally also for syntax highlighting in the editor. This is described in more detail in <a class="reference" href="/doc/custom/qt-styles">Display Style and Colors</a>.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/dark-modeMon, 02 Sep 2019 01:00:00 GMTIntroducing Variables with Refactoring in Wing Prohttps://wingware.com/blog/refactor-intro-var<p>In past issues of <a class="reference" href="/hints">Wing Tips</a> we covered a number of the refactoring operations available in Wing Pro, such as <a class="reference" href="/hints/refactor-rename">renaming symbols</a>, <a class="reference" href="/hints/refactoring-move">moving symbols</a>, and <a class="reference" href="/hints/refactor-introduce">introducing functions and methods</a>. To finish our series on refactoring, let's take a look at how to introduce a variable based on existing Python code, using Wing Pro's <tt class="literal"><span class="pre">Introduce</span> <span class="pre">Variable</span></tt> refactoring operation.</p> <p>This operation is used to replace selected occurrences of an expression with a new local variable, either to make code more readable or to avoid redundant computation.</p> <div class="section"> <h3 class="title-3">Example</h3> <p>Here's a simple example that introduces a local variable <tt class="literal"><span class="pre">tokens</span></tt> to replace repeated use of the expression <tt class="literal"><span class="pre">logical.fTokens</span></tt>, in order to make the code more readable:</p> <img src="https://wingware.com/images/blog/refactor-intro-var/introduce-example.gif" alt="/images/blog/refactor-intro-var/introduce-example.gif" backrefs="" caption="Shown Above: Select the expression &quot;logical.fTokens&quot; to assign to the new local variable, Right-click to initiate Introduce Variable, type in the variable name &quot;tokens&quot;, execute the introduce operation, select the new line of code, and then select the new variable to highlight where it is being used." class="doc-image" dupnames="" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" /><p style="padding-top:0px;"><i>Shown Above: Select the expression "logical.fTokens" to assign to the new local variable, Right-click to initiate Introduce Variable, type in the variable name "tokens", execute the introduce operation, select the new line of code, and then select the new variable to highlight where it is being used.</i></p><p>Notice that Wing replaces all occurrences of the selected expression <tt class="literal"><span class="pre">logical.fTokens</span></tt> with a reference to the new variable <tt class="literal"><span class="pre">tokens</span></tt>. Using refactoring to introduce a new variable is usually much easier and less prone to errors than making edits of this type manually.</p> <p>When only a subset of the instances of an expression should be replaced with the new variable, some matches in the <tt class="literal"><span class="pre">Refactoring</span></tt> tool can be unchecked, or in some cases using <a class="reference" href="/hints/multi-selection">multi-selection</a> may be preferable.</p> </div> <div class="section"> <h3 class="title-3">Try It Yourself</h3> <p>You can easily try this out in your copy of Wing Pro, by selecting any expression in your Python code and choosing <tt class="literal"><span class="pre">Introduce</span> <span class="pre">Variable</span></tt> from the <tt class="literal"><span class="pre">Refactor</span></tt> menu. As in the above example, you will be asked to choose the name of the new variable and Wing will replace all occurrences of the expression with a reference to the new variable. An unwanted introduction can be backed out with the <tt class="literal"><span class="pre">Revert</span></tt> button in the <tt class="literal"><span class="pre">Refactoring</span></tt> tool.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/refactor-intro-varMon, 26 Aug 2019 01:00:00 GMTMoving Code with Refactoring in Wing Prohttps://wingware.com/blog/refactoring-move<p>In this issue of <a class="reference" href="/hints">Wing Tips</a> we explain how to quickly move functions, methods, classes, and other symbols around in Python code, using Wing Pro's <tt class="literal"><span class="pre">Move</span> <span class="pre">Symbol</span></tt> refactoring operation.</p> <p>This operation takes care of updating all the points of reference for the symbol that is being moved. For example, if a function is moved from one module to another then Wing will update all the points of call for that function to import the module it has been moved into and invoke the function from there.</p> <div class="section"> <h3 class="title-3">Example</h3> <p>Here's a simple example that moves a class from one file to another:</p> <img src="https://wingware.com/images/blog/refactor-move/move-example.gif" alt="/images/blog/refactor-move/move-example.gif" backrefs="" caption="Shown Above: Right-click to initiate Move Symbol, select target location testtarget.py, execute the move operation, select the moved function, and then select the added import statement." class="doc-image" dupnames="" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" /><p style="padding-top:0px;"><i>Shown Above: Right-click to initiate Move Symbol, select target location testtarget.py, execute the move operation, select the moved function, and then select the added import statement.</i></p><p>Notice that Wing adds the necessary <tt class="literal"><span class="pre">import</span> <span class="pre">testtarget</span></tt> to <tt class="literal"><span class="pre">testmove.py</span></tt> and changes the two places where the class <tt class="literal"><span class="pre">MyTestClass</span></tt> is used to reference the class in its new location in <tt class="literal"><span class="pre">testtarget.py</span></tt>. Using refactoring is much easier and less prone to errors than making edits of this type manually.</p> </div> <div class="section"> <h3 class="title-3">Try It Yourself</h3> <p>You can easily try this out in your copy of Wing Pro, by selecting any symbol in your Python code base, and choosing <tt class="literal"><span class="pre">Move</span> <span class="pre">Symbol</span></tt> from the <tt class="literal"><span class="pre">Refactor</span></tt> menu. As in the above example, you will be asked to choose the target location for the move, which can either be the top level of any module, or within a class or def. An unwanted move can be backed out with the <tt class="literal"><span class="pre">Revert</span></tt> button in the <tt class="literal"><span class="pre">Refactoring</span></tt> tool.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/refactoring-moveMon, 19 Aug 2019 01:00:00 GMTIntroducing Functions and Methods with Refactoring in Wing Prohttps://wingware.com/blog/refactor-introduce<p>In this issue of <a class="reference" href="/hints">Wing Tips</a> we explain how to quickly create new functions and methods out of existing blocks of Python code, using Wing Pro's <tt class="literal"><span class="pre">Extract</span> <span class="pre">Method/Function</span></tt> refactoring operation.</p> <p>This is useful whenever you have some existing code that you want to reuse in other places, or in cases where code gets out of hand and needs to be split up to make it more readable, testable, and maintainable.</p> <p>Wing supports extracting functions and methods for any selected code, so long as that code does not contain <tt class="literal"><span class="pre">return</span></tt> or <tt class="literal"><span class="pre">yield</span></tt> statements. In that case automatic extraction is not possible, since Wing cannot determine how the extracted function should be called from or interact with the original code.</p> <div class="section"> <h3 class="title-3">Example</h3> <p>Here's a simple example that extracts several lines of code for reuse elsewhere:</p> <img src="https://wingware.com/images/blog/refactor-introduce/extract-example.gif" alt="/images/blog/refactor-introduce/extract-example.gif" backrefs="" caption="Shown Above: Select code to extract, right-click to initiate Extract Function / Method, enter test_extract as the function name, select local nested function as the extraction type, then highlight the extracted point of call, and go to definition of the new nested function." class="doc-image" dupnames="" ids="" names="" style="padding-bottom:5px;margin-bottom:5px;" /><p style="padding-top:0px;"><i>Shown Above: Select code to extract, right-click to initiate Extract Function / Method, enter test_extract as the function name, select local nested function as the extraction type, then highlight the extracted point of call, and go to definition of the new nested function.</i></p><p>Notice that when the code is extracted, Wing takes care of adding the necessary arguments and return values and places an invocation of the new method or function into the original context. This is done in a way that does not alter the functionality of the code, even though it has been restructured.</p> </div> <div class="section"> <h3 class="title-3">Try It Yourself</h3> <p>You can easily try this out in your copy of Wing Pro, by selecting some lines in your Python code base, and choosing <tt class="literal"><span class="pre">Extract</span> <span class="pre">Method/Function</span></tt> from the <tt class="literal"><span class="pre">Refactor</span></tt> menu. As in the above example, you will be asked to enter a name and choose whether to place the extracted code into a function at the top level of the module, in a method in the current class, or in a nested function in the current method or function. An unwanted extraction can be backed out with the <tt class="literal"><span class="pre">Revert</span></tt> button in the <tt class="literal"><span class="pre">Refactoring</span></tt> tool.</p> <p>If you want to move the code to some other module or class, you can do this after initially extracting it by using the <tt class="literal"><span class="pre">Move</span> <span class="pre">Symbol</span></tt> operation in the <tt class="literal"><span class="pre">Refactor</span></tt> menu. We'll take a look at that next time.</p> <br> <br><p>That's it for now! We'll be back soon with more <a class="reference" href="/hints">Wing Tips</a> for Wing Python IDE.</p> </div> https://wingware.com/blog/refactor-introduceMon, 22 Jul 2019 01:00:00 GMTExtending Wing with Python (Part 4 of 4)https://wingware.com/blog/scripting-4<p>In this issue of <a class="reference" href="/hints">Wing Tips</a> we continue to look at how to extend Wing's functionality, by taking a closer look at at the scripting API and writing up a more complex script.</p> <p>If you haven't read the previous installments of this series, you may want to take a look at <a class="reference" href="/hints/scripting-1">Part 1</a> where we introduced Wing's scripting framework and set up auto-completion for the scripting API, <a class="reference" href="/hints/scripting-2">Part 2</a> where we used Wing to debug itself for easier extension script development, and <a class="reference" href="/hints/scripting-3">Part 3</a> where we looked at how to collect arguments from the user.</p> <div class="section"> <h3 class="title-3">Overview of the Scripting API</h3> <p>Wing's formal scripting API is found in the file <tt class="literal"><span class="pre">wingapi.py</span></tt>, which is located inside <tt class="literal"><span class="pre">src</span></tt> in the <tt class="literal"><span class="pre">Install</span> <span class="pre">Directory</span></tt> listed in Wing's <tt class="literal"><span class="pre">About</span></tt> box. This is the API that we worked with in the previous three installments of this series. It lets you control the application and its configuration, run and inspect code in the debugger, open and alter files in an editor, create and manage projects, search files and directories, and access Wing's static analysis of Python code.</p> <p>The API is divided into a number of classes for accessing each of these areas of functionality:</p> <div class="bullet-list"><ul><li><span class="bullet"> <tt class="literal"><span class="pre">CAPIApplication</span></tt> is a singleton found in <tt class="literal"><span class="pre">wingapi.gApplication</span></tt>. This is the the main point of access to the application, with support for creating and accessing windows, editors, projects, tools, the debugger, child processes, preferences, source code analysis, and other parts of Wing's functionality.<p>Some of this functionality is exposed through the other API classes listed below. Other parts of Wing's functionality is instead accessed through the method <tt class="literal"><span class="pre">CAPIApplication.ExecuteCommand()</span></tt>, which can invoke all of the <a class="reference" href="/doc/commands/index">documented commands</a> which are not exposed directly through the Python API. Keyword arguments can be passed to commands that take them, for example <tt class="literal"><span class="pre">ExecuteCommand('replace-string',</span> <span class="pre">search_string=&quot;tset&quot;,</span> <span class="pre">replace_string=&quot;test&quot;)</span></tt></p> <p><tt class="literal"><span class="pre">CAPIApplication</span></tt> also provides access to all of Wing's <a class="reference" href="/doc/preferences/index">documented preferences</a> with <tt class="literal"><span class="pre">GetPreference()</span></tt> and <tt class="literal"><span class="pre">SetPreference()</span></tt>.</p> </span></li> <li><span class="bullet"> <tt class="literal"><span class="pre">CAPIDebugger</span></tt> and <tt class="literal"><span class="pre">CAPIDebugRunState</span></tt> allow controlling the debugger and debug processes.</span></li> <li><span class="bullet"> <tt class="literal"><span class="pre">CAPIDocument</span></tt> and <tt class="literal"><span class="pre">CAPIEditor</span></tt> implement document and editor functionality. Each <tt class="literal"><span class="pre">CAPIDocument</span></tt> may be displayed in one or more <tt class="literal"><span class="pre">CAPIEditor</span></tt> instances in different splits in the user interface.</span></li> <li><span class="bullet"> <tt class="literal"><span class="pre">CAPIProject</span></tt> lets you create projects, access project-level functionality, and alter project configuration.</span></li> <li><span class="bullet"> <tt class="literal"><span class="pre">CAPISearch</span></tt> provides access to Wing's search capabilities, to search files or directories.</span></li> <li><span class="bullet"> <tt class="literal"><span class="pre">CAPIStaticAnalysis</span></tt> is available for each <tt class="literal"><span class="pre">CAPIDocument</span></tt> that contains Python, to access Wing's static analysis of that file. Information on each symbol found in code is encapsulated in <tt class="literal"><span class="pre">CAPISymbolInfo</span></tt>.</span></li> </ul></div><p>Scripts also have access to the entire Python standard library in the version of Python that Wing uses internally to run itself (currently 2.7)</p> <p>The <a class="reference" href="/doc/scripting/api-reference">API Reference</a> details the entire API and documentation is displayed in the <tt class="literal"><span class="pre">Source</span> <span class="pre">Assistant</span></tt> in Wing as you work with <tt class="literal"><span class="pre">wingapi</span></tt>, provided that you configured your project for scripting as described in <a class="reference" href="/hints/scripting-1">Part 1</a> of this series.</p> </div> <div class="section"> <h3 class="title-3">A More Advanced Scripting Example</h3> <p>As a final example, let's look at a simplified test code generator, that creates a <tt class="literal"><span class="pre">unittest</span></tt> testing skeleton for a selected class, opens it side by side with the code being tested, and scrolls to the method being tested as you move through the test skeleton. Here is the complete solution, with comments explaining each part:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">os</span> <span class="kn">import</span> <span class="nn">wingapi</span> <span class="n">g_last_target</span> <span class="o">=</span> <span class="p">[</span><span class="bp">None</span><span class="p">]</span> <span class="k">def</span> <span class="nf">generate_tests_for_class</span><span class="p">():</span> <span class="sd">&quot;&quot;&quot;Generate a test skeleton for the class at the current editor focus, open</span> <span class="sd"> it side by side with the original source, and set up automatic navigation</span> <span class="sd"> of the source being tested.&quot;&quot;&quot;</span> <span class="c"># Collect objects we need to work with: The app and the editor we&#39;re starting from</span> <span class="n">app</span> <span class="o">=</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span> <span class="n">code_editor</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">GetActiveEditor</span><span class="p">()</span> <span class="c"># Try to find a class at current editor focus</span> <span class="n">fn</span><span class="p">,</span> <span class="n">scope</span> <span class="o">=</span> <span class="n">_find_class</span><span class="p">(</span><span class="n">code_editor</span><span class="p">)</span> <span class="k">if</span> <span class="n">scope</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">app</span><span class="o">.</span><span class="n">ShowMessageDialog</span><span class="p">(</span><span class="s">&quot;No Class Found&quot;</span><span class="p">,</span> <span class="s">&quot;Could not generate tests: &quot;</span> <span class="s">&quot;No class was found at current focus&quot;</span><span class="p">)</span> <span class="k">return</span> <span class="c"># Get the methods for the class</span> <span class="n">code_ana</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">GetAnalysis</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span> <span class="n">contents</span> <span class="o">=</span> <span class="n">code_ana</span><span class="o">.</span><span class="n">GetScopeContents</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">scope</span><span class="p">),</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">3.0</span><span class="p">)</span> <span class="n">methods</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">contents</span> <span class="k">if</span> <span class="s">&#39;method&#39;</span> <span class="ow">in</span> <span class="n">contents</span><span class="p">[</span><span class="n">s</span><span class="p">]]</span> <span class="c"># Create the test skeleton, with one test for each public method</span> <span class="n">output</span> <span class="o">=</span> <span class="p">[</span> <span class="s">&#39;import unittest&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="s">&#39;class Test{}(unittest.TestCase):&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="p">]</span> <span class="k">for</span> <span class="n">method</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span> <span class="k">if</span> <span class="n">method</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;_&#39;</span><span class="p">):</span> <span class="k">continue</span> <span class="n">output</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">_indent</span><span class="p">(</span><span class="n">code_editor</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s">&#39;def test{}(self):&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">method</span><span class="p">),</span> <span class="n">_indent</span><span class="p">(</span><span class="n">code_editor</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="s">&#39;pass&#39;</span><span class="p">,</span> <span class="p">])</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">output</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span> <span class="n">output</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">_indent</span><span class="p">(</span><span class="n">code_editor</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span><span class="s">&#39;pass&#39;</span><span class="p">,</span> <span class="p">])</span> <span class="c"># Set up a side-by-side mode after saving the current visual state</span> <span class="n">visual_state</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">GetVisualState</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s">&#39;tools-and-editors&#39;</span><span class="p">)</span> <span class="n">app</span><span class="o">.</span><span class="n">ExecuteCommand</span><span class="p">(</span><span class="s">&#39;unsplit&#39;</span><span class="p">)</span> <span class="n">app</span><span class="o">.</span><span class="n">ExecuteCommand</span><span class="p">(</span><span class="s">&#39;split-horizontally&#39;</span><span class="p">)</span> <span class="c"># Open up the test skeleton in an unsaved file with name based on the original code</span> <span class="n">test_fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">fn</span><span class="p">),</span> <span class="s">&#39;test_{}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">fn</span><span class="p">)))</span> <span class="n">test_editor</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">OpenEditor</span><span class="p">(</span><span class="n">test_fn</span><span class="p">)</span> <span class="n">test_editor</span><span class="o">.</span><span class="n">GetDocument</span><span class="p">()</span><span class="o">.</span><span class="n">SetText</span><span class="p">(</span><span class="n">code_editor</span><span class="o">.</span><span class="n">GetEol</span><span class="p">()</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">output</span><span class="p">))</span> <span class="c"># Connect to the test editor so we can show the matching method in the source</span> <span class="c"># when the caret moves around within the tests</span> <span class="k">def</span> <span class="nf">track_position</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">):</span> <span class="n">lineno</span> <span class="o">=</span> <span class="n">test_editor</span><span class="o">.</span><span class="n">GetDocument</span><span class="p">()</span><span class="o">.</span><span class="n">GetLineNumberFromPosition</span><span class="p">(</span><span class="n">start</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> <span class="n">test_ana</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">GetAnalysis</span><span class="p">(</span><span class="n">test_editor</span><span class="o">.</span><span class="n">GetDocument</span><span class="p">()</span><span class="o">.</span><span class="n">GetFilename</span><span class="p">())</span> <span class="n">scope</span> <span class="o">=</span> <span class="n">test_ana</span><span class="o">.</span><span class="n">FindScopeContainingLine</span><span class="p">(</span><span class="n">lineno</span><span class="p">)</span> <span class="n">scope</span> <span class="o">=</span> <span class="n">scope</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">scope</span><span class="p">:</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">scope</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;Test&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;test&#39;</span><span class="p">):</span> <span class="k">return</span> <span class="n">scope</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="nb">len</span><span class="p">(</span><span class="s">&#39;Test&#39;</span><span class="p">):]</span> <span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="nb">len</span><span class="p">(</span><span class="s">&#39;test&#39;</span><span class="p">):]</span> <span class="n">scope</span> <span class="o">=</span> <span class="n">_find_symbol</span><span class="p">(</span><span class="n">code_ana</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="s">&#39;method&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">scope</span><span class="p">:</span> <span class="k">return</span> <span class="k">if</span> <span class="n">g_last_target</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">scope</span><span class="p">:</span> <span class="k">return</span> <span class="n">g_last_target</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">scope</span> <span class="n">infos</span> <span class="o">=</span> <span class="n">code_ana</span><span class="o">.</span><span class="n">GetSymbolInfo</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">scope</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">infos</span><span class="p">:</span> <span class="k">return</span> <span class="n">lineno</span> <span class="o">=</span> <span class="n">infos</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lineStart</span> <span class="n">start</span> <span class="o">=</span> <span class="n">code_editor</span><span class="o">.</span><span class="n">GetDocument</span><span class="p">()</span><span class="o">.</span><span class="n">GetLineStart</span><span class="p">(</span><span class="n">lineno</span><span class="p">)</span> <span class="o">+</span> <span class="n">infos</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">pos</span> <span class="n">end</span> <span class="o">=</span> <span class="n">start</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="n">code_editor</span><span class="o">.</span><span class="n">ScrollToLine</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">lineno</span><span class="o">-</span><span class="mi">2</span><span class="p">),</span> <span class="n">select</span><span class="o">=</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">),</span> <span class="n">pos</span><span class="o">=</span><span class="s">&#39;top&#39;</span><span class="p">,</span> <span class="n">callout</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="n">test_editor</span><span class="o">.</span><span class="n">Connect</span><span class="p">(</span><span class="s">&#39;selection-changed&#39;</span><span class="p">,</span> <span class="n">track_position</span><span class="p">)</span> <span class="c"># Connect to the test editor so we can restore the prior visual state when it is closed</span> <span class="k">def</span> <span class="nf">restore_state</span><span class="p">(</span><span class="n">unused</span><span class="p">):</span> <span class="n">app</span><span class="o">.</span><span class="n">SetVisualState</span><span class="p">(</span><span class="n">visual_state</span><span class="p">)</span> <span class="n">test_editor</span><span class="o">.</span><span class="n">Connect</span><span class="p">(</span><span class="s">&#39;destroy&#39;</span><span class="p">,</span> <span class="n">restore_state</span><span class="p">)</span> <span class="n">app</span><span class="o">.</span><span class="n">ShowMessageDialog</span><span class="p">(</span><span class="s">&quot;Tests Generated&quot;</span><span class="p">,</span> <span class="s">&quot;Tests for class {} have been generated. &quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">scope</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="s">&quot;As you fill in the tests, the method being tested is shown on the left as you &quot;</span> <span class="s">&quot;move around the test file on the right. Closing the tests will return Wing to &quot;</span> <span class="s">&quot;the previous visual state.&quot;</span><span class="p">)</span> <span class="c"># Command configuration</span> <span class="k">def</span> <span class="nf">_generate_tests_for_class_available</span><span class="p">():</span> <span class="n">ed</span> <span class="o">=</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span><span class="o">.</span><span class="n">GetActiveEditor</span><span class="p">()</span> <span class="k">if</span> <span class="n">ed</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">return</span> <span class="bp">False</span> <span class="n">mime</span> <span class="o">=</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span><span class="o">.</span><span class="n">GetMimeType</span><span class="p">(</span><span class="n">ed</span><span class="o">.</span><span class="n">GetDocument</span><span class="p">()</span><span class="o">.</span><span class="n">GetFilename</span><span class="p">())</span> <span class="k">if</span> <span class="n">mime</span> <span class="o">!=</span> <span class="s">&#39;text/x-python&#39;</span><span class="p">:</span> <span class="k">return</span> <span class="bp">False</span> <span class="k">return</span> <span class="n">_find_class</span><span class="p">(</span><span class="n">ed</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="n">generate_tests_for_class</span><span class="o">.</span><span class="n">available</span> <span class="o">=</span> <span class="n">_generate_tests_for_class_available</span> <span class="n">generate_tests_for_class</span><span class="o">.</span><span class="n">label</span> <span class="o">=</span> <span class="s">&quot;Generate Tests for Class&quot;</span> <span class="n">generate_tests_for_class</span><span class="o">.</span><span class="n">contexts</span> <span class="o">=</span> <span class="p">[</span><span class="n">wingapi</span><span class="o">.</span><span class="n">kContextNewMenu</span><span class="p">(</span><span class="s">&#39;Scripts&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)]</span> <span class="c"># Utilities</span> <span class="k">def</span> <span class="nf">_indent</span><span class="p">(</span><span class="n">ed</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span> <span class="sd">&quot;&quot;&quot;Get indentation that matches the file we&#39;re writing the test for&quot;&quot;&quot;</span> <span class="n">indent_style</span> <span class="o">=</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetIndentStyle</span><span class="p">()</span> <span class="k">if</span> <span class="n">indent_style</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="k">return</span> <span class="s">&#39; &#39;</span> <span class="o">*</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetIndentSize</span><span class="p">()</span> <span class="o">*</span> <span class="n">level</span> <span class="k">elif</span> <span class="n">indent_style</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span> <span class="k">return</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">&#39;</span> <span class="o">*</span> <span class="n">level</span> <span class="k">else</span><span class="p">:</span> <span class="n">tabs</span> <span class="o">=</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetIndentSize</span><span class="p">()</span> <span class="o">*</span> <span class="n">level</span> <span class="o">/</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetTabSize</span><span class="p">()</span> <span class="n">spaces</span> <span class="o">=</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetIndentSize</span><span class="p">()</span> <span class="o">*</span> <span class="n">level</span> <span class="o">-</span> <span class="p">(</span><span class="n">tabs</span> <span class="o">*</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetTabSize</span><span class="p">)</span> <span class="k">return</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">&#39;</span> <span class="o">*</span> <span class="n">tabs</span> <span class="o">+</span> <span class="s">&#39; &#39;</span> <span class="o">*</span> <span class="n">spaces</span> <span class="k">def</span> <span class="nf">_find_symbol</span><span class="p">(</span><span class="n">ana</span><span class="p">,</span> <span class="n">scope</span><span class="p">,</span> <span class="n">symbol_type</span><span class="p">):</span> <span class="sd">&quot;&quot;&quot;Find the outermost symbol of given type in the given scope&quot;&quot;&quot;</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">scope</span><span class="p">)):</span> <span class="n">infos</span> <span class="o">=</span> <span class="n">ana</span><span class="o">.</span><span class="n">GetSymbolInfo</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">scope</span><span class="p">[:</span><span class="n">i</span><span class="p">]),</span> <span class="n">scope</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">infos</span><span class="p">:</span> <span class="k">if</span> <span class="n">symbol_type</span> <span class="ow">in</span> <span class="n">info</span><span class="o">.</span><span class="n">generalType</span><span class="p">:</span> <span class="k">return</span> <span class="n">scope</span><span class="p">[:</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="k">return</span> <span class="bp">None</span> <span class="k">def</span> <span class="nf">_find_class</span><span class="p">(</span><span class="n">ed</span><span class="p">):</span> <span class="sd">&quot;&quot;&quot;Find the current top-level class in the given code editor&quot;&quot;&quot;</span> <span class="k">if</span> <span class="n">ed</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">return</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span> <span class="n">scope</span> <span class="o">=</span> <span class="n">ed</span><span class="o">.</span><span class="n">GetSourceScope</span><span class="p">()</span> <span class="k">if</span> <span class="n">scope</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">return</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span> <span class="n">fn</span><span class="p">,</span> <span class="n">lineno</span> <span class="o">=</span> <span class="n">scope</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="n">ana</span> <span class="o">=</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span><span class="o">.</span><span class="n">GetAnalysis</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span> <span class="n">scope</span> <span class="o">=</span> <span class="n">_find_symbol</span><span class="p">(</span><span class="n">ana</span><span class="p">,</span> <span class="n">scope</span><span class="p">[</span><span class="mi">2</span><span class="p">:],</span> <span class="s">&#39;class&#39;</span><span class="p">)</span> <span class="k">return</span> <span class="n">fn</span><span class="p">,</span> <span class="n">scope</span> </pre></div> </div> <div class="section"> <h3 class="title-3">Key Concepts</h3> <p>The important things to observe in this example are:</p> <p>(1) Wing's source code analyzer is used to find the current class, in the utilities <tt class="literal"><span class="pre">_find_class</span></tt> and <tt class="literal"><span class="pre">_find_symbol</span></tt>: <tt class="literal"><span class="pre">GetSourceScope()</span></tt> is called to get the scope list for the current position in the active editor, and this is inspected with <tt class="literal"><span class="pre">GetSymbolInfo()</span></tt> to find the outermost class in the path to the current scope. For example, if you are within a nested function <tt class="literal"><span class="pre">nested_function</span></tt> in a method <tt class="literal"><span class="pre">MyMethod</span></tt> of class <tt class="literal"><span class="pre">MyClass</span></tt>, your scope is <tt class="literal"><span class="pre">[&quot;MyClass&quot;,</span> <span class="pre">&quot;MyMethod&quot;,</span> <span class="pre">&quot;nested_function&quot;]</span></tt> and the script needs to determine that <tt class="literal"><span class="pre">MyClass</span></tt> is the outermost class.</p> <p>(2) The attributes in the class are found with <tt class="literal"><span class="pre">GetScopeContents()</span></tt> and then narrowed to contain only methods using the filter utility <tt class="literal"><span class="pre">_find_symbol</span></tt> that was also used to find the class.</p> <p>(3) The script uses <tt class="literal"><span class="pre">ExecuteCommand</span></tt> to invoke other Wing commands (<tt class="literal"><span class="pre">unsplit</span></tt> and <tt class="literal"><span class="pre">split-horizontally</span></tt>) in order to set up the side-by-side display mode. This is how you can access all of the <a class="reference" href="/doc/commands/index">documented commands</a>. Since calling these commands changes the layout of the user inteface, the original layout is saved with <tt class="literal"><span class="pre">GetVisualState</span></tt> and restored later.</p> <p>(4) Signal connections made with <tt class="literal"><span class="pre">Connect</span></tt> are used on the editor to hook functionality to specific events in the user interface. In this case, the <tt class="literal"><span class="pre">selection-changed</span></tt> signal on the editor that contains the generated test skeleton is used to scroll the original source to the method that is being tested. The script also connects to <tt class="literal"><span class="pre">destroy</span></tt> on the same editor, in order to terminate the session and restore the original visual layout when the generated test skeleton is closed. Most of the classes in the API provide signals like this. Although there are none in this example, regularly occurring tasks may also be scheduled, using <tt class="literal"><span class="pre">wingapi.gApplication.InstallTimeout()</span></tt>.</p> </div> <div class="section"> <h3 class="title-3">Try it Out</h3> <p>To try the example, copy the following into a new file in the <tt class="literal"><span class="pre">scripts</span></tt> directory in Wing's settings directory (listed in Wing's About box) and use <tt class="literal"><span class="pre">Reload</span> <span class="pre">All</span> <span class="pre">Scripts</span></tt> so Wing discovers and starts watching and reloading the new script file when it changes. A <tt class="literal"><span class="pre">Scripts</span></tt> menu with item <tt class="literal"><span class="pre">Generate</span> <span class="pre">Tests</span> <span class="pre">for</span> <span class="pre">Class</span></tt> should appear in the menu bar. If the menu item does not appear, try restarting Wing (this may be needed on Windows in some versions of Wing).</p> <p>In order for this to work, you will need open a Python file with a class that has public methods (with names not starting with <tt class="literal"><span class="pre">_</span></tt> underscore) and move the caret into the class. If you don't have one, open <tt class="literal"><span class="pre">src/wingapi.py</span></tt> from your Wing installation and use one of the classes in that file.</p> <p>After you invoke the command, it splits the display, places the original code on the left and the test skeleton on the right, and displays a message dialog:</p> <img src="https://wingware.com/images/blog/scripting-4/side-by-side.png" alt="/images/blog/scripting-4/side-by-side.png" backrefs="" class="doc-image" dupnames="" height="658" ids="" names="" width="1092" /><p>If you close the dialog and move the caret around the test skeleton, <tt class="literal"><span class="pre">track_position()</span></tt> in the script will be called each time the editor selection changes in order to track your movement in the original source code like this:</p> <img src="https://wingware.com/images/blog/scripting-4/tracking.gif" alt="/images/blog/scripting-4/tracking.gif" backrefs="" class="doc-image" dupnames="" ids="" names="" /><p>Closing the test skeleton (with or without saving it) will invoke <tt class="literal"><span class="pre">restore_state</span></tt> to return Wing's display to how it was before you invoked the script to generate the skeleton.</p> </div> <div class="section"> <h3 class="title-3">Further Reading</h3> <p>A real world version of this example would need to merge new test classes and methods into an existing file, allow entering test mode without generating a new skeleton, keep tests in file or alphabetical order, add <tt class="literal"><span class="pre">setUp</span></tt> and <tt class="literal"><span class="pre">tearDown</span></tt> methods or other boilerplate, match the active test framework for the current project, and make other refinements. All of these are feasible using Wing's scripting API.</p> <p>For more information on writing extension scripts, see <a class="reference" href="/doc/scripting/index">Scripting and Extending Wing</a> in the documentation.</p> <p>Other example scripts can be found in the <tt class="literal"><span class="pre">scripts</span></tt> directory in your Wing installation, and extensions may be contributed to other users at <a class="reference" href="https://bitbucket.org/sdeibel/wing-contrib">wing-contrib</a>.</p> <br> <br><p>That's it for now! We'll be back in the next <a class="reference" href="/hints">Wing Tip</a> with more helpful hints for Wing Python IDE.</p> </div> https://wingware.com/blog/scripting-4Wed, 10 Jul 2019 01:00:00 GMTExtending Wing with Python (Part 3 of 4)https://wingware.com/blog/scripting-3<p>In this issue of <a class="reference" href="/hints">Wing Tips</a> we continue to look at how to extend Wing's functionality, by taking a look at extension scripts that collect arguments from the user.</p> <p>This article assumes you already know how to add and try out extension scripts in Wing. If you haven't read the previous installments of this series, you may want to take a look at <a class="reference" href="/hints/scripting-1">Part 1</a> where we introduced Wing's scripting framework and set up auto-completion for the scripting API, and <a class="reference" href="/hints/scripting-2">Part 2</a> where we used Wing to debug itself for easier extension script development.</p> <div class="section"> <h3 class="title-3">Script Arguments</h3> <p>Extension scripts may be defined with arguments, which Wing will try to collect from the user if not specified. For example:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">wingapi</span> <span class="k">def</span> <span class="nf">test_argument</span><span class="p">(</span><span class="n">text</span><span class="p">):</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span><span class="o">.</span><span class="n">ShowMessageDialog</span><span class="p">(</span><span class="s">&quot;Result&quot;</span><span class="p">,</span> <span class="s">&quot;You typed: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">text</span><span class="p">))</span> </pre></div> <p>After the script is loaded, you can use <tt class="literal"><span class="pre">Command</span> <span class="pre">by</span> <span class="pre">Name</span></tt> from the <tt class="literal"><span class="pre">Edit</span></tt> menu to execute it by typing <tt class="literal"><span class="pre">test-argument</span></tt> as the command name. Because the type of argument <tt class="literal"><span class="pre">text</span></tt> is undefined, Wing collects it as a string in an entry area shown at the bottom of the IDE window:</p> <img src="https://wingware.com/images/blog/scripting-3/arg-collection.png" alt="/images/blog/scripting-3/arg-collection.png" backrefs="" class="doc-image" dupnames="" height="35px" ids="" names="" width="247px" /><p>In key bindings and added toolbar items, it is often useful to provide the value of an argument as part of the invocation like this: <tt class="literal"><span class="pre">test-argument(text=&quot;hello&quot;)</span></tt></p> <p>This displays the message dialog without first prompting for the value of <tt class="literal"><span class="pre">text</span></tt>:</p> <img src="https://wingware.com/images/blog/scripting-3/arg-collection-result.png" alt="/images/blog/scripting-3/arg-collection-result.png" backrefs="" class="doc-image" dupnames="" height="118px" ids="" names="" width="163px" /><p>Script arguments must always be passed using the keyword <tt class="literal"><span class="pre">arg=value</span></tt> form. If a script has multiple arguments and only some are specified in its invocation, Wing collects only the unspecifed arguments from the user.</p> <p>Default values may be specified in the script definition, in order to avoid collecting the argument from the user if it is not given in the invocation:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">wingapi</span> <span class="k">def</span> <span class="nf">test_argument</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s">&quot;default&quot;</span><span class="p">):</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span><span class="o">.</span><span class="n">ShowMessageDialog</span><span class="p">(</span><span class="s">&quot;Result&quot;</span><span class="p">,</span> <span class="s">&quot;You typed: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">text</span><span class="p">))</span> </pre></div> <p>This simplest form, without specifying argument type or interface, is sufficient for many scripting tasks that require argument collection.</p> </div> <div class="section"> <h3 class="title-3">Argument Type and Interface</h3> <p>Extension scripts may also specify the type and interface to use for arguments by by setting the <tt class="literal"><span class="pre">arginfo</span></tt> function attribute on the script. This is a map from argument name to a specification that includes documentation, data type, argument collection interface, and a label.</p> <p>This example collects two arguments, a filename and a choice from a popup menu:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">wingapi</span> <span class="kn">from</span> <span class="nn">wingutils</span> <span class="kn">import</span> <span class="n">datatype</span> <span class="kn">from</span> <span class="nn">guiutils</span> <span class="kn">import</span> <span class="n">formbuilder</span> <span class="k">def</span> <span class="nf">test_arg_entry</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">word</span><span class="p">):</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">gApplication</span><span class="o">.</span><span class="n">ShowMessageDialog</span><span class="p">(</span><span class="s">&#39;Choice {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">word</span><span class="p">),</span> <span class="s">&quot;You chose: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span> <span class="n">_choices</span> <span class="o">=</span> <span class="p">[</span> <span class="p">(</span><span class="s">&quot;Default&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span> <span class="bp">None</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;One&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&quot;Two&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s">&quot;Three&quot;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="p">]</span> <span class="n">test_arg_entry</span><span class="o">.</span><span class="n">arginfo</span> <span class="o">=</span> <span class="p">{</span> <span class="s">&#39;filename&#39;</span><span class="p">:</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">CArgInfo</span><span class="p">(</span> <span class="s">&quot;The filename to enter&quot;</span><span class="p">,</span> <span class="c"># The tooltip shown to use over this field</span> <span class="n">datatype</span><span class="o">.</span><span class="n">CType</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">),</span> <span class="c"># The data type is string</span> <span class="n">formbuilder</span><span class="o">.</span><span class="n">CFileSelectorGui</span><span class="p">(),</span> <span class="c"># Use a file selection field to collect the value</span> <span class="s">&quot;Filename:&quot;</span> <span class="c"># The field label</span> <span class="p">),</span> <span class="s">&#39;word&#39;</span><span class="p">:</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">CArgInfo</span><span class="p">(</span> <span class="s">&quot;The word to enter&quot;</span><span class="p">,</span> <span class="n">datatype</span><span class="o">.</span><span class="n">CType</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">),</span> <span class="n">formbuilder</span><span class="o">.</span><span class="n">CPopupChoiceGui</span><span class="p">(</span><span class="n">_choices</span><span class="p">),</span> <span class="c"># Use a popup menu to collect this value</span> <span class="s">&quot;Word:&quot;</span> <span class="p">)</span> <span class="p">}</span> <span class="c"># This causes the script to be listed in a new menu Scripts in the menu bar</span> <span class="n">test_arg_entry</span><span class="o">.</span><span class="n">contexts</span> <span class="o">=</span> <span class="p">[</span><span class="n">wingapi</span><span class="o">.</span><span class="n">kContextNewMenu</span><span class="p">(</span><span class="s">&#39;Scripts&#39;</span><span class="p">)]</span> </pre></div> <p>Notice that this imports some additional modules not used in our previous examples in this series: <tt class="literal"><span class="pre">datatype</span></tt> is used to specify the type of an argument, and <tt class="literal"><span class="pre">formbuilder</span></tt> is used to specify how the value is collected from the user. These are documented in <a class="reference" href="/doc/scripting/arginfo">Argument Collection</a> in the users manual.</p> <p>Once you load the script, you can try it with <tt class="literal"><span class="pre">Test</span> <span class="pre">arg</span> <span class="pre">entry</span></tt> in the <tt class="literal"><span class="pre">Scripts</span></tt> menu that should appear in the menu bar. The value collection area will look like this:</p> <img src="https://wingware.com/images/blog/scripting-3/argentry.png" alt="/images/blog/scripting-3/argentry.png" backrefs="" class="doc-image" dupnames="" height="64px" ids="" names="" width="288px" /><p>Pressing <tt class="literal"><span class="pre">Tab</span></tt> moves between the fields and <tt class="literal"><span class="pre">Enter</span></tt> executes the command, which displays this dialog:</p> <img src="https://wingware.com/images/blog/scripting-3/argentry-result.png" alt="/images/blog/scripting-3/argentry-result.png" backrefs="" class="doc-image" dupnames="" height="118px" ids="" names="" width="220px" /></div> <div class="section"> <h3 class="title-3">Using a Dialog for Argument Entry</h3> <p>Arguments may also be collected in a dialog, rather than at the bottom of the IDE window. For the above example, this would be done by appending the following code:</p> <div class="python-highlight"><pre><span class="n">test_arg_entry</span><span class="o">.</span><span class="n">flags</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;force_dialog_argentry&#39;</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span> </pre></div> <p>After this change is saved, executing <tt class="literal"><span class="pre">Test</span> <span class="pre">arg</span> <span class="pre">entry</span></tt> from the <tt class="literal"><span class="pre">Scripts</span></tt> menu displays the argument entry fields in a dialog instead:</p> <img src="https://wingware.com/images/blog/scripting-3/dialog-argentry.png" alt="/images/blog/scripting-3/dialog-argentry.png" backrefs="" class="doc-image" dupnames="" height="122px" ids="" names="" width="400px" /></div> <div class="section"> <h3 class="title-3">Providing History and Completion</h3> <p>Argument entry can also implement history (accessible by pressing the up and down arrows) and completion of matching choices with <tt class="literal"><span class="pre">Tab</span></tt> or <tt class="literal"><span class="pre">Enter</span></tt>. The following example does both for its argument <tt class="literal"><span class="pre">text</span></tt>:</p> <div class="python-highlight"><pre><span class="kn">import</span> <span class="nn">wingapi</span> <span class="kn">from</span> <span class="nn">wingutils</span> <span class="kn">import</span> <span class="n">datatype</span> <span class="kn">from</span> <span class="nn">guiutils</span> <span class="kn">import</span> <span class="n">formbuilder</span> <span class="c"># The command does nothing other than adding the argument to history</span> <span class="c"># (first=most recent)</span> <span class="n">_history</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">def</span> <span class="nf">test_arg_entry</span><span class="p">(</span><span class="n">text</span><span class="p">):</span> <span class="k">if</span> <span class="n">text</span> <span class="ow">in</span> <span class="n">_history</span><span class="p">:</span> <span class="n">_history</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> <span class="n">_history</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span> <span class="c"># For completions, we just use the matching words in this file</span> <span class="k">def</span> <span class="nf">_completions</span><span class="p">(</span><span class="n">fragment</span><span class="p">):</span> <span class="n">fn</span> <span class="o">=</span> <span class="n">__file__</span> <span class="k">if</span> <span class="n">fn</span><span class="o">.</span><span class="n">endswith</span><span class="p">((</span><span class="s">&#39;.pyc&#39;</span><span class="p">,</span> <span class="s">&#39;.pyo&#39;</span><span class="p">)):</span> <span class="n">fn</span> <span class="o">=</span> <span class="n">fn</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="n">txt</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="k">return</span> <span class="nb">sorted</span><span class="p">([</span><span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">txt</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">fragment</span><span class="p">)])</span> <span class="n">test_arg_entry</span><span class="o">.</span><span class="n">arginfo</span> <span class="o">=</span> <span class="p">{</span> <span class="s">&#39;text&#39;</span><span class="p">:</span> <span class="n">wingapi</span><span class="o">.</span><span class="n">CArgInfo</span><span class="p">(</span> <span class="s">&quot;Test with completion and history&quot;</span><span class="p">,</span> <span class="n">datatype</span><span class="o">.</span><span class="n">CType</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">),</span> <span class="n">formbuilder</span><span class="o">.</span><span class="n">CSmallTextGui</span><span class="p">(</span><span class="n">history</span><span class="o">=</span><span class="n">_history</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">_completions</span><span class="p">)</span> <span class="p">),</span> <span class="p">}</span> <span class="n">test_arg_entry</span><span class="o">.</span><span class="n">contexts</span> <span class="o">=</span> <span class="p">[</span><span class="n">wingapi</span><span class="o">.</span><span class="n">kContextNewMenu</span><span class="p">(</span><span class="s">&#39;Scripts&#39;</span><span class="p">)]</span> <span class="n">test_arg_entry</span><span class="o">.</span><span class="n">flags</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;force_dialog_argentry&#39;</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span> </pre></div> <p>After replacing the script created earlier with the above code you should see an auto-completer as you type:</p> <img src="https://wingware.com/images/blog/scripting-3/auto-completion.png" alt="/images/blog/scripting-3/auto-completion.png" backrefs="" class="doc-image" dupnames="" height="188px" ids="" names="" width="415px" /><p>After executing the command one or more times, the up and down arrows traverse the stored history:</p> <img src="https://wingware.com/images/blog/scripting-3/history.gif" alt="/images/blog/scripting-3/history.gif" backrefs="" class="doc-image" dupnames="" height="188px" ids="" names="" width="415px" /><p>Note that in this implementation the history is lost each time the script is reloaded. One way to save history or any other value across script reload or sessions is to store it using <tt class="literal"><span class="pre">SetAttribute()</span></tt> on the current project obtained from <tt class="literal"><span class="pre">wingapi.gApplication.GetProject()</span></tt>.</p> <p>The above covers most argument collection needed by extension scripts for Wing. A few other data entry methods are also supported, as documented in <a class="reference" href="/doc/scripting/arginfo">Argument Collection</a> in the users manual.</p> <br> <br><p>That's it for now! In the next <a class="reference" href="/hints">Wing Tip</a> we'll take a look at the API in more detail, in order to write a more complex scripting example.</p> </div> https://wingware.com/blog/scripting-3Fri, 28 Jun 2019 01:00:00 GMT