ArcGIS Blog

Analytics

Using Python and QML to build native apps

By Lucas Danzinger

Python is a highly efficient and intuitive language that is pervasive throughout the ArcGIS Platform, as well as the larger scientific community. I personally love writing Python code because you can build sophisticated scripts that accomplish a wide array of tasks with relatively little effort or lines of code. Many ArcGIS users have picked up the scripting language in recent years and have found it to be invaluable in maximizing their productivity. However, one thing that I think most Python coders would agree with me on is that it isn’t very easy to make great looking UIs with Python. There are options, such as TkInter, that allow you to build up UIs from Python, but I would argue that the power of Python is in scripting procedural workflows, not in building beautiful UIs. There are, however, other languages and frameworks for building great UIs that integrate well with Python, and one of those is Qt.

Qt is a cross platform SDK that is similar in functionality to .NET, and provides APIs in both C++ and QML. For those that are not already familiar, QML is a declarative JavaScript based language that looks similar to JSON with nested JavaScript functions within it. QML is a newcomer to the ArcGIS stack, and recently emerged as one of the languages used in the ArcGIS Runtime SDK for Qt, as well as AppStudio for ArcGIS. QML is great for building user interfaces, and through a plugin called PyOtherSide, integrates very well with Python. This plugin allows you to call Python scripts directly from QML. This is powerful for a few reasons:

  • Python programmers now have a simple and intuitive way to build great looking UIs to execute their Python scripts.
  • Qt/ArcGIS Runtime programmers can now leverage the power of Python and its many different packages (ArcPy, NumPy, matplotlib, Pandas, etc) in their native apps.
  • It allows Python and QML to do what each language is best at, and separates the UI from the business logic.
  • This could be a great bridge to Python developers wanting to learn ArcGIS Runtime technology, but not wanting to totally abandon their Python skills.

Building off of these different use cases, I put together a GitHub repo that shows some samples of how to integrate QML, Python, and ArcGIS through PyOtherSide. The examples include:

  • A simple hello world sample that shows how to call a Python function
  • An ArcPy integration sample, where a map is displayed with ArcGIS Runtime. On a mouse click, the latitude and longitude from the click event is passed into a Python function that uses ArcPy’s geometry operations.
  • An elevation profile sample, where the z values of a polyline in ArcGIS Runtime are plotted through Python’s matplotlib.
  • An Excel conversion sample, where an Excel spreadsheet is converted to CSV in Python, and is then published as a Feature Service in ArcGIS Online through the ArcGIS Runtime QML API.
Here is a snippet that shows how a Python function can be executed from QML. Note how a list of parameters can be passed in, and a callback JavaScript function is passed in. This will be executed on completion of the Python script.
Here is the Python script that is executed by the above QML code. It converts an Excel spreadsheet to CSV and returns the output path.

These samples only scratch the surface of possible applications for this technology. However, I think they serve as a good starting point to understand how QML works (if you don’t already know) and how to call Python functions from QML. Whether you are a Python dev looking to build UIs or an ArcGIS Runtime dev looking to utilize your Python skills in your native apps, PyOtherSide is a powerful and intuitive plugin that opens the doors for many ArcGIS users. Please note that specific issues relating to integrating ArcGIS and PyOtherSide is not supported by Esri technical support (but don’t let that stop you from seeing what you can create!).

Share this article

Subscribe
Notify of
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments