Run local python script from Google Apps Script - python

As the title suggests, I'm looking for a way to run local python scripts from the Google Apps Scripts environment. Right now my method is to write a json file from app scripts and save it to a Google Drive directory. The local machine polls the directory via Google Drive File Stream, and runs the necessary code when the file appears in the directory. Not pretty, but it gets the job done. The main concern with this is that File Stream can be fairly latent - could be up to 15 minutes after a file is put in the directory before File Stream catches it and syncs the local machine.
Is anybody aware of other options? I could try re-writing the python code in Apps Script, though the python code relies on a few third party libraries that aren't available in JS. Another idea is setting up a flask server, but that seems like a good bit of work for the return.

Related

Can Google Apps Script call a python script?

I have a python script that uses a python api to fetch data from a data provider, then after manipulating the data, writes some of that data to Google Sheets (via the Google Sheets python api). So my workflow to update the data is to open the python file in VS Code, and run it from there, then switch to the spreadsheet to see the updated data.
Is there a way to call this python script from Google Sheets using Google Apps Script? If so, that would be more efficient; I could link the GAS script to a macro button on the spreadsheet.
Apps Script runs in the cloud at Google's servers rather than in your computer, and has no access to local resources such as Python scripts in your system.
To call a resource in the cloud, use the URL Fetch Service.
Currently, I am using following hack.
The appsscript behind gsheet command button writes parameters in a sheet called Parameters. And the python function on my local machine checks that Parameters sheet in that google workbook every 5 seconds. If there are no parameters, then it exists. And if there are parameters, then it executes the main code
When the code is deployed on service account, the portion which is polling remains inactive. And the appsscript directly makes a call directly to python code in service account.
There are many reasons why I need to call python function on LOCAL machine from gsheet. One reason is --- debugging is better in local machine and cumbersome on service account. Another reason is --- certain files can be put on local machines and we do not want to move these files to workspace. And gsheet needs data from these files.
This is a HACK I am using
.
I am looking for a better way that this "python code keeps polling" method.

ASP.NET webpage on IIS Server does not call Python/run scripts at all

I've developed an ASP.NET website on IIS (virtual Windows Server using Amazon Lightsail) which works as expected for the most part. But one issue with it is with running specialized Python scripts.
In my website, there is a feature where a user (of a certain account level) can upload an image, which is then passed through Python (through Process.Start). After Python does its thing, it sends text output back to the webpage. This works perfectly under the context of IIS Express in my local computer, but on the server, it seems to not run Python at all.
It is noted that I did not receive any warnings or errors. Seems to entirely skip the Process.Start segment.
I suspect that it is due to either of these:
Security access
The application pool
The Process.Start function
Things I tried:
Changing the security settings of the folder with the Python app and the scripts, as well as the Python executable
Changing the application pool of the website to LocalSystem
Disabled dynamic compression
Added a script map for the Python app
Reinstalled Python from Users/Administrator/Local/Appdata... to C:/Python (also changed the required paths in the website code behind) I also made sure that I installed the right Python libraries for the code to work.
Apart from the file paths in the code behind, nothing is changed in the actual ASP.NET website and DLL. Ideally I should not need to change anything else with regards to the website.
The website still does not call Python. I've been scratching my head for the past two days on this single issue.
Also, the folder where the Python scripts and related files are located are within wwwroot.
Any suggestions are much appreciated.
TL;DR: ASP.NET website that calls Python to run an already-coded script as part of its function works great under IIS Express. Python does not run when called under IIS Server.

How to run a python script on client side without demanding permissions or requiring special installations?

I have a windows application built with progress openedge technology.
I have created a python script to generate an excel file but I need to deploy it to the client and im afraid of requiring special permissions on the client side if I compile it to .exe and attempt to run it.
Can someone suggest me a method to be able to integrate python with my project smoothly without breaking anything?
You could compile it on your own machine then try to run it while logged in as a guest user. If a guest account can run it without complaints it will probably run fine on the client machine.
This is crude because you still haven't tested all possible client platforms (unless you're talking about one specific client), also we don't know what's inside your script.
Use icacls to set appropriate permissions of your compiled script before shipping.
I'm not sure about the special permissions thing, but is it possible for you to turn your script into a CGI program and stick it on your webserver, or wrapper it with WebSpeed? Then your app could call a web service to get the .xls file.

How to Access text files uploaded to Heroku that is running with Python Script

apologies upfront. I am an extreme newbie and this is probably a very easy question. After much trial and error, I set up an app on Heroku that runs a python script that scrapes data off of a website and stores it in a text file. (I may switch the output to a .csv file). The script and app are running on a Heroku Scheduler, so the scraping takes place on a schedule and the data automatically gets written to the file that is on the Heroku platform. I simply want to download the particular output file occasionally so that I can look at it. (Part of the data that is scraped is being tweeted on a twitter bot that is part of the script.)
(Not sure that this is relevant but I uploaded everything through Git.)
Many thank in advance.
You can run this command heroku run cat path/to/file.txt, but keep in mind that Heroku uses ephemeral storage, so you don't have any guarantee that your file will be there.
For example, Heroku restarts your dynos every 24 hours or so. After that you won't have that file anymore. The general practice is to store files on some external storage provider like Amazon S3.
Not just ephemeral, but immutable, which means you can't write to the file system. You'll have to put the file in something like S3, or just put the data into a database like Postgres.

How to get the App Engine SDK to track file changes in directories added via sys.path?

I am currently experiencing an issue with the Google AppEngine SDK where directories added using sys.path don't have their changes recognized by the SDK. Requiring the SDK be restarted in order for the modified files to have any effect.
This is particularly a problem with frameworks that make use of sys.path, such as tipfy.
I've been dealing with file monitoring for some time and wrote
a library to do this. Hence a shameless plug:
Write a wrapper script around the dev_appserver module entry point
to restart it using a library like:
https://github.com/gorakhargosh/watchdog
or the nosy script at
https://github.com/gorakhargosh/watchdog/blob/master/scripts/nosy.py
HTH.
Cheers,
Gora Khargosh.

Categories