Search code examples
javascriptpythonhtmlweb2py

How do i write a generator function in html view in web2py with no return?


I'm trying to print the remote terminal logs in a html modal,and i intend to achieve this using generator function & writing it down in the view but how do i write it since the generator function does not return anything and hence no keyword return. Also it gives me a constant error ---SocketClosed: Client closed socket.

------This is in the view test.html------

{{def generator():}}
        {{import subprocess}}
        {{Username = 'username'}}
        {{Password = 'password'}}
        {{IP = 'hostname'}}
        {{Connection_type = '-ssh' #can have values -ssh -telnet -rlogin -raw -serial}}
        {{import sys}}
        {{from subprocess import *}}
        {{proc = Popen(['plink', Connection_type, '-l', Username, '-pw', Password, IP], shell=True, stdin=subprocess.PIPE,stdout=PIPE)}}
        {{proc.stdin.write("WORKSPACE=/export/abc \n export WORKSPACE \n")}}
        {{proc.stdin.write("cd /export/ \n")}}
        {{proc.stdin.write("pwd \n")}}
        {{proc.stdin.write("./xyz.sh \n")}}
        {{while True:}}
            {{data = proc.stdout.readline(50)   # Alternatively proc.stdout.read(1024)}}
            {{yield data}}
            {{if len(data) == 0:}}
              {{break}}
            {{pass}}
        {{pass}}
{{return none}}
{{extend 'layout.html'}}


<!-- Button trigger modal -->

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
          <p>Hello There
        {{data=generator()}}}}
        {{for i in data:}}
        {{=i}}
        {{pass}
          </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <!--<button type="button" class="btn btn-primary">Save changes</button>-->
      </div>
    </div>
  </div>
</div>

P.S.I'm using generator for the reason that otherwise my code is stuck in an infinite while loop until the whole of the process is completed.


Solution

  • Python code in web2py templates is executed entirely on the server before the HTML page is returned to the browser. No Python code can be executed in the browser. Therefore, this method will not work for dynamically displaying data from the server within the web page.

    Once the web page has been loaded, if you need to retrieve new data from the server to display in the page, you'll need to make an Ajax call or set up some kind of server-push solution, such as websockets or server-sent events.

    web2py includes some basic websocket functionality, which requires the Tornado web server.