If you are looking to start building web apps with HANA on the backend quickly using Python, this is the right blog for you! For this, you need to have the following set up:
Once you have your environment ready, you can try the web.py tutorial out. I found the module easy to use for the purpose of simple webpage-rendering. The tutorial can be found here: http://webpy.org/docs/0.3/tutorial. This tutorial will also help you get accustomed to the web.py framework features. Basically, all you need to to do is:
In the end your code should look something like this:
import web
import pyodbc
# ask template engine to render html files from the templates/ directory
render = web.template.render('templates/')
# set url handler
urls = (
'/', 'index'
)
# create a remote hana db connection through odbc
conn = pyodbc.connect('DSN=<odbc_connection_name>;UID=<user>;PWD=<password>')
# create a cursor for executing queries
c = conn.cursor() #mysql cursor
# render views for index page
class index:
def GET(self):
c.execute("SELECT * FROM <schema>.<table> ORDER BY <field>") # hanadb execution
todos = c.fetchall()
return render.index(todos) # render the index files forwarding the todo parameter
# boiler plate syntax for running the main method
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
Make sure you have followed and understood the Web.py tutorial before you jump into this piece of code. I saved this code in a file called code.py.
I created a simple index template which looks like this (/render/index.html😞
$def with (todos)
<ul>
$for todo in todos:
<li id="$todo[0]">$todo[1]</li>
</ul>
This template file just renders the views to show you a list of results it passed through the todo variable.
Now to try it out:
C:\Users\UserName\Document\webpy> python code.py
Check your browser to see if the page is visible or not by visiting http://localhost:8080. You should get a simple webpage with your records from HANA.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
34 | |
25 | |
12 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 |