How To Call Python script from AngularJS App - python

We using AngularJS as a frontend for our web application and some of the functions we are using python to do the calculation and get results back.
I would like to know is there any way to calling the python script directly in AngularJS? right now we are using $HTTP service to call PHP then in PHP using EXEC command to call the python, it is all working fine.
The problem is we notified there is about 5 seconds delay every time the python script call and I guess it is because of the overhead for the python interpreter and try to start it every time, we would like to eliminate that delay.
We are run on Redhat v 6.8 / AngualarJS 1.4x and Python 3.6 Anaconda3
Does anyone try something like that? any suggestions are welcome.
Thank you!

You could write a python method that calls your calculation code and expose that method as a REST API.
You would need a library like Flask.
This tutorial explains how to do that : https://www.codementor.io/sagaragarwal94/building-a-basic-restful-api-in-python-58k02xsiq
This way you can directly call the python API.

Related

Best way to run python script alongside php and html

I have a web server which I have developed an application on using php and SQL, mainly picked php as I am more comfortable with it.
In short the application automates some of our network tasks .
As part of this I have automated some solarwinds tasks and the library orionsdk doesnt have a php library so I have used python.
It's all working fine but I really need to run these python scripts from my browser .
I have considered using php shell exec and got my python scripts to accept args so I can run them and parse the output.
I know I could also use flask or django but worry I will have a flask app to maintain aswell as a php app.
I think the question is what the best way to achieve this or any way which I haven't mentioned .
Any help would be very much appreciated
So you want PHP to communicate with Python and you've already mentioned using shell commands and http traffic.
I can imagine you could also achieve something similar by connecting up both PHP and Python up to the same database. In that case PHP could write a record in a table and Python could pick that up and do something with the data in there. Python could be either be a long-running process or fired off by a cronjob in this case. If a database seems overkill you could also write a file to some place on disk, which Python can pick up.
Personally I'd go for the shell exec approach if you want to keep it light weight and for a API connection if you want to have a more robust solution which needs to be expanded later on.

Need advice on how to incorporate Python into an Azure, specifically an ASP.NET web application environment

Need advice on how to incorporate Python into an Azure ASP.NET web application environment. Please excuse this question but I am new to Azure and I'm not clear on how to proceed. Every option that I look into looks promising but they all seem to have their own issues. Below is a more thorough explanation but the deal is that I have an Azure account with all kinds of goodies, a full fledged ASP.NET (C#) web app running via App Service, I am new to Azure (but not Python), and I'm hoping to add Python functionality to this whole setup. In short:
I want to add Python to this setup mainly to run scheduled jobs and also to trigger Python code from ASP.NET web form submissions
ideally I want a solution that resembles a non-cloud setup. I know this sounds silly but I'm finding the cloud/Azure functionality to be nuanced and not straightforward. I want a place to put a bunch of Python scripts, run, edit, schedule and trigger them from ASP.NET
for example: I created a WebJob that runs manually and from the documentation it wasn't clear how it should be called. I just figured out that you need to POST with Basic Auth (and the credentials provided).
!Also, Azure CMD does NOT like files with 'underscore _' in them! You cannot submit a Web Job with a py file with an underscore nor can you write output with a file with an underscore
!Also, I don't see an option for this Web Job to run Python 3.6.4 (which I installed via extension). Right now it is using 2.7.15...
!Also, CRON expression in Azure has six *, not five plus a command. Again, more weird stuff to worry about
I tried these instructions but the updates to the web page's Web.config file breaks the ASP.NET web pages
ideally the most cost effective option
Any info is greatly appreciated
MORE DETAILED EXPLANATION
Currently I have an ASP.NET site running via Azure App Service and I would like to add Python scripts and possibly Flask/Rest functionality. Note that I am not expecting to serve any content via Python and will largely be running Python scripts either on a scheduled basis or call them from ASP.NET. As a matter of fact, and this is an important point, I'm hoping to have ASP.NET trigger/run a Python script when a web form is submitted. I realize that I could get a similar effect if I make a web call to a Rest api that is running Python. In any event, I can't tell if I should:
add a Python extension to the current App Service running the web page (I tried this) OR
I did install Python 3.6.4 and some packages via pip
These instructions were useful, however the updates to the web page's Web.config file breaks the ASP.NET web pages
set up a VM that will have all of the Python code (but how can I have the .NET web page(s) call the Python in the VM?) OR
use Azure functions (I'm completely new to this and must admit that I prefer to have my old school Python environment instead although I see the benefit of using functions. But how do you deal with logging and debugging?)
or what about a custom windows container (Docker)?
This requires installing VS Code and that is OK but I'm looking for a solution that another user can get into with as few interruptions as possible
The idea is to ramp up the use of Python although, like I said, I don't expect Python to be serving any of the web content. It will be used to run in the background and to run scheduled jobs. What is the most robust and hopefully easiest way to add Python functionality to Azure (most importantly in a way to be able to trigger/use Python from an App Service running .NET?)? I've searched online and stack overflow so far with interesting finds but nothing to my liking.
For example, the following link discusses how to schedule WebJobs. I just created a manual one and when I called the webhook I got the message: "No route registered for '/api/triggeredwebjobs/TestPython/run'" How to schedule python web jobs on azure
The Docker method looks very promising, however, I'm looking for a simple solution as there is another person who will be involved in all of this and he's busy with other projects
Thank you very much!
I found a solution, though I'm open to more info. Like I mentioned in my post, I used the 'add extension' tool to add Python 3.6.4 to my Azure (installed in D:\home\python364x64).
Then I installed a bunch of packages via pip, these installed into D:\home\python364x64\Lib\site-packages.
I created a Python folder in webpages\Python where I put my scripts.
Finally, in ASP.NET I used the Diagnostics.Process call to run my code in ~\webpages\Python\somecode_2.py
The main issue is that Azure came with Python 2.7.15 installed. And for some reason when my Python code got executed it was using 3.4 (where that version came from beats me). So for each script, I had to create an _2.py version where I simply did the following in order to call the original script via Python 3.6.4. Looks a little nasty but it works. So like I said, I would welcome more info for ways to do this better...
--
import os<br>
os.system("D:\\home\python364x64\python.exe SomePython.py {0}".format(add arguments here)

Run Python in background of NodeJS

I'm not sure what I'm trying to do is possible, but I'm trying to write a NodeJS application that needs to call some Python functions, the way I'm going to use Python functions is similar to How to call Python function from NodeJS by using child_process
If the python script had a few imports that had a slight delay when running the script, if you were calling the python script often surely it would cause problems for the applications running time. Is there a way to get around to constantly keep a child process python call open and then call a function whenever it's needed?
Thanks
There's nothing specific to Python nor node.js here. If you don't want the overhead of spawning a child process, make the "other" script / application / whatever a long running process and use any kind of inter-process communication.

migrate python code to NodeJS

I am a beginner in NodeJS. I have worked on some scripts in python which does some calculation on two csvs. Is there a easy way to migrate a python script to NodeJS? I don't know whether its a right way to do it. But I wanted to know is there any way for for it.
Any suggestions will be encouragable.
If you simply want to convert the code to javascript there are tools available like transcrypt which converts the python code to javascript.
Simple way : understand your code and rewrite it in node.js format.
If you're a beginner in node.js, it's a good exercise to understand how node.js works.
I recommand you this post which regroup a lot of ressources to begin node.js : How do I get started with Node.js
EDIT : you can also execute your Python script with a node.js process, and use the result in node.js.
Documentation : https://nodejs.org/api/process.html

python send commands to interpreter from script and get result

My goal is to create a webpage the uses python and flask. I would like to have the ability to enter some commands into this web page, submit them to flask which will execute the commands in a python interpreter. Flask will then retrieve the results and send them back to the web page for presentation.
How can this be done? I currently am able to execute unix shell commands but cannot for the life of me figure out how to send commands to a python interpreter and retrieve the results.
I guess I should have clarified the purpose. This is for an internal web application for the company that I work for. Only employees will be able to access it while on our network and only after proper authentication has been performed. The employees need a way to have an interactive shell with the particular machine that they are logged in to through their web browser.
SOLUTION:
I found a nice module name Pexpect that does exactly what I want. Thank you for all of your suggestions.
The following code uses flask and pexpect module to call python interpreter.
#app.route('/console')
def console():
import pexpect
child = pexpect.spawn('python')
child.expect('\n>>>')
child.sendline('import os')
child.expect('\n>>>')
Well, you can invoke python in the same way as you do a shell script and pass python code to it using the -c option. See http://docs.python.org/2/using/cmdline.html
However, this is unbelievably insecure and I would not recommend doing it in a web app!
If you are set on this, read up on restricted execution in Python http://docs.python.org/2/library/restricted.html
The Werkzeug debugger that is included with Flask can do this when there is an exception, so I think it should be fairly easy to base your solution on that code, or at least to learn how Werkzeug does it.
The debugger server side code is here. The client side is here.
I hope you have a good reason to do this, you normally do not want random people to execute code on your server.

Categories