32736

Can Django use “external” python scripts linked to other libraries (NumPy, RPy2…)

Question:

I am new to the world of IT business (serious) development but I have in mind a business idea and still trying to vizualize how the overall infrastructure should work.

I have done some few research for a good technology to deliver the solution. I am very inclined to use Python, MySql, Django (Apache) on the server side and some RIA on the client side (probably Flex) as I need some advanced visualization capabilities (especially after seeing the FLARE project).

The application requires some "heaving lifting" on the numerical / statistical side and integrating R with Python (RPy2) + other like NumPy seems to be ideal.

The thing i cannot get so far (certainly because i am a newbie) is the following:

Can Django (one way or the other) execute an (external) python script / program which contain reference to the extra libraries (NumPy ...)?

ex: user triggers an action to perform a statistical analysis, Django receives the request and should run some python code (using R, NumPy...) which uses the data in the database and store the results back in the DB. Django accesses the DB data and send it back to the client app to be displayed.

Is this the right logic or am i completely off path?

Many thanks in advance for your expertise.

Answer1:

Django is a Python program. And like any other Python program it will be able to access other Python scripts/modules. The question then, is how to execute the script. If your script explicitly defines a main (or another starting point) function then you can merely import it as you would a module and call the main.

For instance:

# my custom script. Located in my_script.py # lots of functions def main(): # call functions in sequence. # my django view. from myscript import main as script_main script_main()

If you'd rather execute as if from the command line then look at the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a> module. If you want to run it asynchronously then something like <a href="http://celeryproject.org/" rel="nofollow">Celery</a> might be what you are looking for.

Answer2:

If you can install it on the server and import it into python, then you can use it in python and hence Django.

That is to say, if

import foo

works, then so does

import foo foo.bar(fobaz)

assuming that it would work <em>without</em> Django. Also, if you were to try to do something that sent HTTP headers or responses outside of Django, you might run into problems but numerical packages wont do anything like that.

Recommend

  • Hierarchical (Multi-column) Sorting for the .net GridView?
  • Why is parser combinator “seq” defined with “bind” and “return”?
  • python pandas- AttributeError: 'Series' object has no attribute 'columns'?
  • State management in ReactJS
  • Slide panel up after x seconds, close on click - SET COOKIE
  • R mlogit model, computationally singular
  • parsing XML configuration file using Etree in python
  • WPF DataGrid lost focus after row delete
  • How do I control the soft menu button in Honeycomb?
  • Hudson dependencies
  • Perl keyword say is not working in version 5.14.4
  • What dll is needed for Windows.Devices.Geolocation?
  • Laravel include causes error: Method Illuminate\\View\\View::__toString() must not throw an exce
  • Cassandra: What is a subcolumn
  • Netezza Incremental load from Sql server using SSIS
  • composer dependency stating in doesn't have php-xsl
  • LNK1104: cannot open file 'kernel32.lib'
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • python: forcing relative imports to search from script file
  • Converting query results into DataFrame in python
  • import scipy.sparse failed
  • vectorized indexing/slicing in numpy/scipy?
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Security issues with PHP's Readfile method
  • Firefox Extension - Monitor refresh and change of tab
  • onBackPressed() not being executed
  • How to delay loading a property with linq to sql external mapping?
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • JavaScriptCore crash on iOS9
  • Importing jscolor library in angular 2
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Delete MySQLi record without showing the id in the URL
  • Matplotlib draw Spline from multiple points
  • A cron job substitute?
  • Does armcc optimizes non-volatile variables with -O0?
  • Programmatically clearing map cache