Running Python scripts within MySQL Workbench

In my previous blog post I explained how to enable Python in the MySQL Workbench GRT Shell. This blog explains how to run a simple Python script using Workbench. You don’t need to change any preferences to make this work.

Save the following little script to a file called wbscript.py. It will print the tables in the first schema of the first model opened.

import grtmodel = grt.root.wb.doc.physicalModels[0]
for tab in model.catalog.schemata[0].tables:
	print "Table: `%s`" % (tab.name)

To see the output of the script make sure that:

  1. you have a model open (with at least 1 table)
  2. the Output Window is visible: View > Advanced > Output Window
  3. make the Output option active in the Output Window (defaults to Messages)

In Workbench, chose Tools > Run Workbench Script and locate the script. Open it and it will run immediately (successfully, hopefully!). The output should be something like this (name of table could be something else):

Executing script /Users/geert/Projects/mysql/wbscript.py...

Table: `t1`Script finished.

Running Workbench scripts is easy, but more awesome would be to make your own plug-in! This is however a different beast that I’ll hopefully cover in next posts.