0

I write this simple example: I have a function x = 100*Math.random() that print a number every t = 1000 ms. Ok, it works.

My trouble is: how do I use/call an external python function (e.g. x = counter.py) instead of x = 100*Math.random()?

somepage.html

    ... 
    <script  src="{% static 'jquery-1.11.2.js' %}"></script>
    <script src="{% static 'js/contaBIS.js' %}"></script>
    <button type="button" class="btn btn-success" id="start"> START </button>
    <p id ="A"></p>
    <p id ="B"></p>
    ...

contaBIS.js

$(document).ready(function() {
var x;                                      
var t = 1000;


    $('#start').click(function() {

        $('#start')
            .removeClass('btn-success')
            .addClass('btn-warning disabled')
            .text('WAIT'); 

        var myVar=setInterval(function(){myTimer()},t);
        function myTimer() {

        document.getElementById("A").innerHTML="something's working ";

        x = 100*Math.random();         //just an example

        document.getElementById("B").innerHTML= x ;
        }

});

counter.py

import time
z = 0
def prova(z):


    while z < 10:
        time.sleep(1)
        z = z + 1
        print(z)      // I see z only on Eclipse console
    return z

Ok, HERE I saw how to call an external function but I don't know how see its values on html. Do I have to write another view to do it? how?!

I look at print the value of a variable in Python/Django? but it is not good for me.

Community
  • 1
  • 1
Trix
  • 587
  • 1
  • 6
  • 27

2 Answers2

2

The browser cannot execute any random code on the web server (where Django runs), it can only download pages.

My first bet is to create a Django view that actually runs counter.py and renders its output to something the browser can process.

On your viewer page you should include an AJAX call that gets the result of the beforementioned view.

Note that, however, that your counter.py doesn't store any state information, which means every time you run it, you will get the same output:

0
1
2
3
4
5
6
7
8
9

All this rendered in 10 seconds, which is really poor response time for an AJAX call (in fact, for any page load).

GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
1

Ok I solved part of my issue changing several things. I post another question to be more specific and because I can't delete this one. I quote it here, hoping it will be useful to people like me who has just started studying django:

Clicking on a html button, I call views.stream_response which "activates" views.stream_response_generator which "activates" stream.py and return a StreamingHttpResponse and I see a progressive list every second up to n at /stream_response/:

[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

stream.py

import time             
def streamx(n):
    list = []  
    x=0
    while len(list) < n:      
        x = x + 1
        time.sleep(1)
        list.append(x)
        yield "<div>%s</div>\n" % list
    return list

views.py

def stream_response(request):
    n = 10         #I would like to take this value from a FORM on html
    resp = StreamingHttpResponse( stream_response_generator(n))
    return resp    


def stream_response_generator(n):
    res = stream.streamx(n)
    return res

urls.py

...
url(r'^homepage/provadata/$', views.provadata),    
url(r'^stream_response/$', views.stream_response, name='stream_response'),
...

homepage/provadata.html

<a href="{% url 'stream_response' %}" class="btn btn-info" role="button" id="btnGo">GO</a>
Community
  • 1
  • 1
Trix
  • 587
  • 1
  • 6
  • 27