r/PHPhelp • u/smoke4sanity • Mar 13 '17
I would like to create a PHP web application, which has a mathematical analysis portion best suited to python or R. What's the best way to incorporate either Python or R?
Hi,
Im building an app using Laravel (only on the modelling phase) and Im just thinking how easy/hard it would for users to enter data, maybe large amounts of data, have calculations performed in R or Python, have the results in either graphs or text passed back to the user in the Python web app.
Could someone please point me in the right direction to determine the best way I could do that? Or would there be a different web progmming language I could use? THanks!
1
u/theantichris Mar 13 '17
Would creating an API in Python then just using PHP to query and display the results work?
3
u/smoke4sanity Mar 13 '17
You know what, thanks for the response. Considering 40% of my program would need to be in Python/R, I think I will just develop the entire web application in Python.
1
u/holyshock Mar 14 '17
If you want to do it all with one language, go with Python since there are web dev packages for that language. However, I would second building a Python API and querying with PHP.
1
u/smoke4sanity Mar 14 '17
How would that work? Like I create my webpage and functionality in PHP, and then when I need to do calculations as well as exploratory data analysis, call the Python API and just plug into an iframe or something?
1
u/holyshock Mar 14 '17
Just about. You can use guzzle or curl to get the API response inside your PHP code before the page renders, so you don't need the iframe. Sounds to me like you should also get to know what JSON is and how it works. Very simple way to make dealing with calls between computers/languages easy.
4
u/Meefims Mar 13 '17
It depends on the user scenarios. Do the users need results in realtime? Will they instead come back later for a daily/weekly/whatever analysis? How expensive is the processing?
Assuming the analysis is fairly expensive and you would like to use Laravel then you can offload the analysis work to a Python worker using something like RabbitMQ for jobs that need immediate processing. You can give your client the job ID that it can use to find out if the analysis has finished. If you can do the daily roll up then your Laravel app can stash the unprocessed data away and a scheduled task running Python can process any unprocessed data and put the results in some known location that your Laravel app can then read from.