Python toolboxes are a great way to create custom, GUI toolboxes for ArcGIS without actually interacting with the GUI (since everything can be defined within the pyt file). Regrettably (and incomprehensibly) documenting a python toolbox does require the GUI (see Documenting a tool in a Python toolbox). This help page also states:
For Python toolboxes, the documentation for the toolbox and tools are stored in .xml files that are associated with the toolbox and tools by name.
In a question on this topic the user @dmahr stated in his answer:
Fortunately, Python supports for reading and writing XML, so you should be able to dynamically edit the help text from a separate script.
How would something like this be achieved? Since my toolbox is constantly changing, I'd like code and documentation to live in the same file.
EDIT:
As @user2856 commented, the first step would be to look at the xml file generated when documenting the tool via the GUI. This step should have been part of the question, so here's the xml for the tool named "Tool" (saved with the name New Python Toolbox.Tool.pyt.xml
:
<?xml version="1.0"?>
-<metadata xml:lang="de">
-<Esri>
<CreaDate>20190401</CreaDate>
<CreaTime>10300800</CreaTime>
<ArcGISFormat>1.0</ArcGISFormat>
<SyncOnce>TRUE</SyncOnce>
<ModDate>20190401</ModDate>
<ModTime>14352000</ModTime>
-<scaleRange>
<minScale>150000000</minScale>
<maxScale>5000</maxScale>
</scaleRange>
</Esri>
-<tool xmlns="" toolboxalias="" displayname="Tool" name="Tool">
<arcToolboxHelpPath>c:\program files\arcgis\pro\Resources\Help\gp</arcToolboxHelpPath>
-<parameters>
-<param displayname="Input Features" name="in_features" expression="in_features" datatype="Feature Layer" direction="Input" type="Required">
<dialogReference><DIV STYLE="text-align:Left;"><DIV><P><SPAN>this is the in_features dialog explenation </SPAN></P></DIV></DIV></dialogReference>
<pythonReference><DIV STYLE="text-align:Left;"><DIV><P><SPAN>and here is the scripting explenation</SPAN></P></DIV></DIV></pythonReference>
</param>
</parameters>
<summary><DIV STYLE="text-align:Left;"><DIV><P><SPAN>This is a nice summary</SPAN></P></DIV></DIV></summary>
<usage><DIV STYLE="text-align:Left;"><DIV><P><SPAN>This is how this tool should be used</SPAN></P></DIV></DIV></usage>
</tool>
-<dataIdInfo>
-<idCitation>
<resTitle>Tool</resTitle>
</idCitation>
-<searchKeys>
<keyword>Here are some tags</keyword>
</searchKeys>
</dataIdInfo>
-<distInfo>
-<distributor>
-<distorFormat>
<formatName>ArcToolbox Tool</formatName>
</distorFormat>
</distributor>
</distInfo>
-<mdHrLv>
<ScopeCd value="005"/>
</mdHrLv>
<mdDateSt Sync="TRUE">20190401</mdDateSt>
</metadata>
Best Answer
This was bothering me probably more than it should have, and after getting to know python a little bit I've found that the problem is pretty easy to solve. Since I'm mostly an R user, these lines of python code might not be very pythonic. Feel free to comment / edit:
EDIT: Since the whole script is fairly long I've added everything to a Github Repo: https://github.com/ratnanil/PythonToolboxDocumentation
Step 1: Adding attributes within .pyt File
Add the documentation to the .pyt File as attributes to the objects. For example, adding adding a "Dialog Reference" for an individual Parameter can be created like so:
You are free to choose your own names for the attributes (eg
dialogref
), just make sure you are not overwriting existing names (name
,direction
,value
etc.).Step 2: Create a python Script to extract the tool's documentation and write xml Files
This file will hold the code to extract all the documentation from the .pyt File and write the xml files based on this information.
Step 3: Create a python Script to extract the toolbox's documentation and write xml Files
Now you have created the most important part of the documentation (for each tool). In a similar manner, you can now create the documentation for the toolbox itself.