Run a Python Script saved online

662
5
10-31-2022 12:21 PM
AngelRomoSandoval
Occasional Contributor

Hello,

I have a Python script that access ArcGIS Online and perform a couple of operations in a Feature Layer. 

I have only Basic ArcGIS License.

My idea is to save this script online, somewhere, for example, in this website and allow the user to, by clicking in a link, execute the script.

The problem is that the scripts uses the following libraries: 

from arcgis.gis import GIS
from
arcgis.geometry import Geometry
from arcgis.geometry.filters import intersects

I have installed the libraries with Conda: 

https://support.esri.com/en/technical-article/000022005

But I do not know if or how can I upload these libraries to this online websites. It is possible?

0 Kudos
5 Replies
Brian_Wilson
Occasional Contributor III

I tried doing "pip install arcgis" at Pythonanywhere and it used my apparently tiny allocation of CPU seconds and says I have to wait 24 hours to get more. HA ha ha! Well, it's free!

I keep a virtual machine at Tektonic.net. But you just get a command prompt there so you have to know Linux commands. On the other hand I get the resources I need for $4/month. Vultr.com is popular too but a few $ more.

I have already installed conda there, so something like this gets arcgis installed and usable for me,

# This creates an environment and installs python, arcgis module, and jupyter notebooks
conda create --name=arcgis -c esri arcgis jupyter
# This activates the environment
conda activate arcgis
# Now you can start Jupyter, and you can start typing Python interactively.
jupyter console

Then I can start typing in commands like "from arcgis.gis import GIS"... or I can copy a script up there and run it instead with "python myscript.py"

My new "arcgis" environment stays up there on the virtual machine, ready to go whenever I need it, I just have to issue the command "conda activate arcgis" to load it.

Normally to run a tiny web server to run some Python I would use Flask. I can also run my own Jupyter Notebook server. This is quite a bit cheaper than a license for the ArcGIS Notebook server. So for me, it is worth the extra learning curve problems of working at the command line.

 

0 Kudos
Brian_Wilson
Occasional Contributor III

I should also mention, the commands shown here work on a Windows machine, so you can get started testing and running code locally on a Desktop and once you have something working you can copy the Python up to the virtual machine. I use Visual Studio Code on my desktop to run code both locally and remotely -- you can set up a session and run a Jupyter notebook using the local desktop or using the remote virtual machine.

0 Kudos
Brian_Wilson
Occasional Contributor III

Searching for ways to host Python online for free (or cheap), I found Binder.

Binder is not exactly what you're asking for but very cool. They let you upload and run notebooks, apparently 100% for free. It looks like it is a project of the Leibniz Institute for Social Sciences.

To load my test project, I set up a simple project and then saved it to a git repository and then fed the URL to mybinder.org

My code is at https://github.com/brian32768/hello-binder

You can try running it here https://mybinder.org/v2/gh/brian32768/hello-binder/main?labpath=index.ipynb

I set it up in Visual Studio Code and it will show me the map there, sadly it does not show up in Binder. But you asked if you could run geometry operations so I added a cell for that. Yes, it worked.

from arcgis.geometry import Geometry, Point
from arcgis.geometry.filters import intersects
pt = Point({"x":-123, "y":46.6, "spatialReference": {"wkid":4326}})
print(pt)

 

0 Kudos
Brian_Wilson
Occasional Contributor III

screenshot from Binderscreenshot from Binder

0 Kudos
AngelRomoSandoval
Occasional Contributor

Thank you for all the info, BrianWilson7,

I do not have so much knowledge as you, so I am not able to follow all your different methods, I am trying now with the last one: Git-Hub + Binder, but I think that it does not work for me because I need to have an open Gibthub repository and I need it to be Private.

I have also tried on my own Replit  but when I tried to load the ArcGIS API for Python packages I get always an error. This option would be the perfect because I can have all private and I can trigger the script from anywhere.

Also I have created a Notebook in ArcGIS Online that is run each 15 minutes, but there is no option, as far as I know, to be able to have somewhere a link to trigger the script.

Thank you anyway for your help,

Angel

0 Kudos