ArcGIS, HTML and Python via Pyscript

Is it possible to create a webpage that loads a arcpy library and displays a mapping application? It just might be possible now with the introduction of py-script.

  • Jason Tromborg

For many years, Javascript has been the go-to language for developing websites; however, HTML has only just added a new tag called pyscript>, which makes it possible for developers to execute Python code directly within HTML. For Python users, and particularly for those working in the GIS community, this is a significant step forward.

PyScript: A Brief Introduction
PyScript is a robust framework that allows developers to use Python to construct web apps using an HTML interface. The framework's goal is to give users access to a top-notch programming language that follows standard graphical standards, is more expressive, and is simpler to learn.


PyScript's ability to give GIS developers, who are already proficient in Python's use for data processing and analysis, with a language that is already familiar to them is one of the most significant advantages of utilizing PyScript. They may simply incorporate their Python-based analyses into web apps by making use of PyScript, which eliminates the need for them to learn a new programming language.

Can PyScript Replace Javascript?
PyScript is an impressive framework; yet, it is not expected to completely supplant Javascript any time soon. On the other hand, it is far more effective than Javascript at replacing several big loads of data. PyScript's continued relative slowness in comparison to Javascript is one of the most significant drawbacks of using this programming language. PyScript has a download size requirement of 22 megabytes, and even with cache turned on in the browser, the process might still take longer than most web developers are comfortable with.

The most effective method for integrating HTML with Python at the present is to make use of a framework such as Django. PyScript, on the other hand, has a great deal of promise to propel the usage of Python in GIS and has the ability to someday become a big role in web development as its speed and efficiency continue to improve.

Example: PyScript for GIS
Let's have a look at one example of how PyScript may be utilized in GIS so that we can better understand the capability of this language. Take, for example, a web application that has the need of displaying a map that contains many levels of data. In the past, this would call for a significant amount of intricate Javascript code to be written in order to correctly manage the data and display it. PyScript makes it possible to accomplish this goal with only a couple of lines of Python code.

    import arcpy # Load data layers streets = arcpy.FeatureLayer('streets.shp') buildings = arcpy.FeatureLayer('buildings.shp') # Create a map object and add layers map = arcpy.mapping.MapDocument('map.mxd') arcpy.mapping.AddLayer(map, streets) arcpy.mapping.AddLayer(map, buildings) # Export the map to an image arcpy.mapping.ExportToPNG(map, 'map.png')    

PyScript can be used to create a map by doing the following: pyscript> import arcpy

# Load data layers
streets are an equivalent to arcpy.
FeatureLayer('streets.shp')
arcpy stands for "buildings."
FeatureLayer('buildings.shp')

# Initiate the creation of a map object and add layers using the arcpy.mapping command.

MapDocument('map.mxd') \sarcpy.mapping.

arcpy.mapping has a function called AddLayer(map, streets).

AddLayer(map, buildings) (map, buildings)

# Create an image of the map using arcpy.mapping.ExportToPNG(map,'map.png'), and then close the pyscript.

<!— Show the map picture --> <img src="map.png">
This piece of code imports the PyScript library and then makes use of it to generate a map with two layers: streets and buildings. The map is then exported as a PNG image, which is the format used for the image that is presented on the web page. PyScript may be used to streamline the building of GIS websites, making the process both quicker and more effective as demonstrated by this example.

Conclusion
PyScript is a robust framework that has the potential to revolutionize the way Python developers engage in web development. This shift might be for the better. Although it is possible that it will not completely replace Javascript, it does give a strong tool for GIS developers and has a significant potential to increase both speed and efficiency as it continues to grow.