Python program in an external script

38 views
Skip to first unread message

Jorge Manrique

unread,
Oct 3, 2017, 8:58:59 PM10/3/17
to sage-cell
Hello everyone,
I have an interactive program in Python. The program does some calculations with SageMath and graphs the results. The code is a bit long.
I want to embed the program on a webpage using SageCell and I wonder if I can leave the Python program on the web server and use an external script:

<head>
. . . . . . .
  <script>
     sagecell.makeSagecell ({
          inputLocation: '#plot'
     })
  </ script>
</ head>
. . . . . . .
<div id = "plot">
  <script type = "text / x-sage" src = "/ src / somegraph.py"> </ script>
</ div>

or compile the program, for example, with CPython, and then include the bytecode in the second script above:

<div id = "plot">
  <script type = "application / octet-stream">
      bytecode
  </ script>
</ div>

Any suggestions will be appreciated,
Jorge

Andrey Novoseltsev

unread,
Oct 5, 2017, 10:56:45 PM10/5/17
to sage-cell
Have you tried these? I am quite sure that bytecode will not work since both JS and Python side of SageMathCell operate with clear text code. Loading files into script tags perhaps may work. What I know some people have done in the past was loading long code via some short code snippet that uses standard Python means of loading files/code, but that relies on having your code hosted somewhere with a stable IP or DNS name.

Jorge Manrique

unread,
Oct 6, 2017, 10:15:47 PM10/6/17
to sage-cell
Hello Andrey
neither of them works.
As I read here, if the MIME type is text/x-sage then the src attribute is ignored and the code will be treated as an embedded data block that will not be processed by the browser.
In relation to the bytecode, as you mention, it does not make sense.

I'll try to use the makeSagecell callback attribute to load the program. AJAX style :-)

If it works I will post it here
Thanks again,

George

Jorge Manrique

unread,
Oct 17, 2017, 8:56:14 PM10/17/17
to sage-cell
Hello there
I found a solution:
<html>
<head>
  
<script>
        sagecell.makeSagecell({
          inputLocation: '#pyscript',
          template: sagecell.templates.minimal,
        });
  </script>
  <script src="sagesrc.js"></script>
</head>
<body onload="sagesrc()">
  ......
  
<div id="pyscript">
        <script type="text/x-sage" src="somepgm.py"></script>
  </div>
  .......
</body>
</html>

The script:sagesrc.js:
function getPyScr(s) {
    "use strict";
    var xhr;
    
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            s.innerHTML = this.responseText;
        }
    };
    xhr.open("GET", s.src, false);  //If async is true it does not work
    xhr.send();
}

function sagesrc() {
    "use strict";
    var scrObj, scrObjLen, i;
    
    scrObj = document.scripts;
    scrObjLen = scrObj.length;
    
    for (i = 0; i < scrObjLen; i++) {
        if (scrObj[i].type === "text/x-sage" && scrObj[i].src != "") { 
            getPyScr(scrObj[i]);
        }
    }
}

If the Python script, as in my case, is large it will take some time to load
But I can write it once and use it several times and in different places

Obviously, any improvement and / or comment is welcome
Jorge

Andrey Novoseltsev

unread,
Oct 18, 2017, 1:06:23 AM10/18/17
to sage-cell
On Tuesday, 17 October 2017 18:56:14 UTC-6, Jorge Manrique wrote:
Hello there
I found a solution:

Great!

If the Python script, as in my case, is large it will take some time to load
But I can write it once and use it several times and in different places

Can you please elaborate on this "will take some time to load"? The largest code you can submit to SMC is 64K, so how getting such a file can significantly affect the page load time nowadays?.. And do you mean that there is a difference between this approach and just copy-pasting Sage code into HTML?

Thank you!

Jorge Manrique

unread,
Oct 18, 2017, 4:24:59 PM10/18/17
to sage-cell
Hello Andrey,
I have not made any measurements of the runtime environment, so I can not objectively compare both approaches.
I think if the overhead time of the XMLHttpRequest is insignificant in relation to the load time of the embedded python script and the script is used several times, this approach could be used

Ciao,
Jorge
Reply all
Reply to author
Forward
0 new messages