How do i import python functions to web2py

281 views Asked by At

My python example code:

def test_print():
    code.....
    print('Message posted')
test_print()

How do I import: test_print() to a HTML view in web2py?Do I have to alter the python code? my aim is to print the message on HTML

1

There are 1 answers

1
Matheus CAS On

I think you should not do like that way you wrote. See the following section from web2py's book: http://www.web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules. Import your module "test_print.py" into your controller and passes the function's results to your view. Something like this:

In your controller:

import test_print
def message():
    result = test_print.test_print()
    return dict(result_to_view=result)

In your view "message.html":

<html>
      <head></head>
      <body>
            <h1>My message is: {{=result_to_view}}</h1>
      </body>
</html>

Got it?