integrating python scripts with django - python

I have written some test scripts in python which call some apis for a paritcular application and output the results to a database so I can use jasper to report on the results. Currently the scripts are run using a python interpreter, ie double click on the python file that has some parameters and variables modifed within the script that then initiates the tests. I would like to move towards a more user friendly interface so other people can use these test scripts without having to modify the python code. So I am thinking about using django and creating a web page that has check boxes, and if the check box is ticked, then it will execute that particular python test script or a text box that will pass the values to a given variable, for example. I have a few questions around how this might be achieved.
1 - would I need to implement the python code in to the django source code or can I call the python script from the web page served up by django?
2 - If I were to run this from a web page, how could I ensure that if the web page was closed, that the test would continue in the background.
3 - Is there a way to output the status of the test case to a web page and if the web page was closed, for the status to be availble if the web page was reopened?
Many thanks - oli

If you have a python function you can call from a Django django view maybe with a form as parameter input. If you have long running processes you might want to consider a tip from here: How to start a long-running process from a Django view?
from mytests import testfunc
def test_parameter_view(request):
if request.method == 'POST': # If the form has been submitted...
form = ParameterForm(request.POST)
if form.is_valid():
testfunc(form.cleaned_data['parameter']) # <-- Here the actual testing happens
return HttpResponseRedirect(reverse(test_result)) # Redirect after POST
else:
form = ParameterForm()
return render_to_response('test.html', {
'form': form,
})
In your test_result view you can access the test result values from the database.
If the user closes the browser or not doesn't affect server processes that already have been started. And since you write your results to the database they are persistent and can be accessed any time after the test has finished.

If you don't want to port over your scripts into django views, there is another way:
1 - Set up a form with all the options that you want passed to the script
2 - GET or POST the form params and save them to variables using var1 = request.POST['param1'], etc
3 - Use a module called subprocess to execute your script. http://docs.python.org/library/subprocess.html

Related

How can i update a django template in real time?

In this template i'm retrieving the price of Bitcoin from an API. At the actual moment, the price will be updated only when the page is refreshed, while i would like it to be updated without refreshing the whole page, dynamically.
This is my view:
def home(request):
symbol = "BTCUSDT"
tst = client.get_ticker(symbol=symbol)
test = tst['lastPrice']
context={"test":test}
return render(request,
"main/home.html", context
)
And the template's line looks something like this:
<h3> var: {{test}} </h3>
There are two problems here:
1) From the little i know, Django itself is not asynchronous, so i need to find a way to update that part of the template in real time, without having to refresh the whole page.
2) At the actual moment, the API is requested when the page is opened/refreshed, but to stream the price, it should be always running. I tried this (awful) solution: add a while true in the view, but of course it broke my code, executing only the part the while statement.
Any advice is appreciated :)
You should separate your frontend and backend in order to dynamically update the DOM contents without needing to render the whole DOM each time.
The responsibility of frontend would be to request and fetch the latest values when a user does an action that warrant a refresh or fetching of updated data. The request need to made asynchronously via e.g. AJAX. The newer JS frameworks e.g. React, Vue use virtual DOM that use an intermediate virtual DOM that they use to push the updates and finally update the real DOM in a single go; which makes them very performant.
The (Django) backend should expose an API that would take (AJAX) requests from backend for specific resources, and serve them.
A nice framework for Django is DRF (Django REST Framework), which exposes REST endpoints that you can call from frontend via AJAX and get reponse back so that the frontend can do necessary updates.
This is a failrly high level view, and is written to give you an idea that you can implement by digging deeper.

Export a File (CSV, PDF) From Django Admin Form Inputs

All within the Django admin, I'd like to enter form fields related to generating a report. Fields you'd expect in a report: report name, report type, start and end dates, report fields, etc.
How would one take these inputs, grab the inputs from the request, pass these inputs to an API (in the background), then process it (in queue-like fashion), finally create a CSV or PDF to download?
I'm fine with creating the admin model form and I think grabbing the inputs when the form is submitted in the admin, then I think I simply pass those inputs to my other API code to process...
My questions are:
When the third-party API is processing the request, is there a special way to handle this lag time?
Where and how would I return the result - which is a CSV or PDF - in the admin interface? The /change/ page?
Is there best-practice for this? I haven't been able to find an example of this when dealing with the admin. I'm not new to Python but am somewhat new to Django.
Nobody will likely answer. Therefore, I'll share my high-level general solution.
You'll need to setup celery.py and modify your __init__.py like it shows in celery's documentation. The key "gotcha" is where they put "proj", you should put the actual name of your project, not "proj".
In your admin.ModelAdmin class, override save_model(self, request, obj, form, change) to capture the form data submitted from the admin form.
Use the form inputs and pass to a celery task function in tasks.py within the current app. Ex: get_report.delay(name, start, end, type). Note the .delay part as this is what allows it to run in the background without having to wait at the screen after you submit the form.
In save_model(...), you still have to call super().save_model(request, obj, form, change) at the end.
The most important step of all - you have open two terminals! 1 for running python manage.py runserver (for example) and another for starting the celery worker. I'm on Windows and Windows has issues so a non-concurrent solution is celery -A scorecard worker --pool=solo -l info (to start the worker - you type this in Terminal), OR, you can install eventlet and use eventlet as the --pool=eventlet value instead of --pool=solo.

How to access Models.py from external py script?

I want to integrate my existing code into django. How can I access models.py from external code.I tried putting it in the same application directory but it did not work.
Case Scenario: Having a web parsing code that takes data from the web and prints it. Here I am integrating it with django so as to save that data in the db and print it out in the web page. I'm trying the input to get from the webpage and when I hit submit it triggers the parsing code and gives output on the page.
I have the Django server up and running, created the apps (startapp), have the table created via models.py, have the urls.py setup. Here my question is how or where do I put my external code to make it trigger to access models .py, will it execute by itself when I start the server and hit the submit button ( i know i have to configure this one).
There are a few lines you need to include in your script if you want to use Django in "standalone" mode. See the documentation here: https://docs.djangoproject.com/en/1.10/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage .
Another option would be to turn your script into a Django management command which you would then call with manage.py.

creating a python web page

I am working on a small task.
the task requires me to process the data from a html form. the html form consists of a phone dial pad. the data is the phone number the user enters. I need to process the data. The functionality for processing of phone number (data) has been completed.
But i have a problem. I am used to django framework. But for this task, the database is not needed and i do not need a admin page as well. In short, i do not want to use a framework for a single web page.
Is there any possible ways for me to create a simple html form, and when i submit the form the data is transferred to the python code for processing.
I have been looking over through the internet, and i am not finding any suitable ways of implementing it. can you please help me out or point me in the right direction
I don't have an exact idea about Python Flask but you'll have to do the something similar to the following
from flask import Flask
app = Flask(__name__)
#app.route("/")
def calculation():
#manipute request here as per your
#pass manipulated data to the processing functionality already present
if __name__ == "__main__":
app.run()
Suppose this code now runs at http://localhost/, in HTML code, you'll have to write a form processing JavaScript code that'll parse the form. You can use any method .serialize() or anything else depending on what you are comfortable with. Then make an Ajax call to the running Flask code and to it pass the request parameters.
You can then probably host this Flask code on Heroku or something similar. Here is method that's creating RESTful web API using Flask. You can read through it and understand the workings of it. http://blog.luisrei.com/articles/flaskrest.html

Ploneformgen dynamically populate selection field

I am trying to customize a ploneformgen form by dynamically populating a selection field. I need to parse a file on my filesystem and select certain options based on their date and time. Creating the python script was no brainer but then where do I place the script?
From what I understand, external methods are not the preferred choice in plone 4
I tried to add a python script via ZMI but then I was hit by restricted python and my script would not execute.
I developed an add-on and placed it in the eggs folder and then wrote a python script to call the code from the add-on but unfortunately I was hit again with the same error as previously "You do not have sufficient permissions to view this page". From what I understood, code inside an add-on is not restricted, or?
What would be the best option to achieve the customisation of the form?
If you used a skin script, then yes, that is run as restricted python as well. It is the filesystem-stored equivalent of the ZMI python script.
Best practice is to use a browser view; it is simply a callable object that has a request and context associated to it:
from zope.publisher.browser import BrowserView
class MyBrowserView(BrowserView):
def __call__(self):
request = self.request
context = self.context
# Do something with the request and context
Register this in your configure.zcml:
<browser:view
for="*"
name="mybrowserview"
class=".views.MyBrowserView"
permission="zope2.Public"
/>
after which it'll be available as someobject/##mybrowserview for PloneFormGen.
However, if this is the only thing you are creating a custom package for then by all means use an external method.

Categories