A hello-world HANA web application using Web.py
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:
- Given that you already have Python installed, you need the web.py module installed from here: http://webpy.org/
- Install the pyodbc module to load the ODBC driver for HANA connectivity
- You need to have a working ODBC connection on your system and test it out with your remote HANA server
- A text editor or an IDE for coding in Python
- Some data to retrieve from the HANA server. Follow this tutorial on how to use the HANA Studio to create tables and some dummy data: http://www.experiencesaphana.com/docs/DOC-1721
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:
- Copy and past some boiler plate syntax in to your web.py home directory
- Import all the necessary libraries for Python (we will be using the pyodbc module in this example)
- Set the template rendering path
- Set the routes
- Initialise DB connections and create a cursor
- Define the classes (as pages) and methods (as actions)
- Fire up the built in web.py server and visit the page you just created
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.
Voted it up a bit. Not an in-depth article after all, but it's definitely worth reading in order to get things "together" and get started. And it gives web.py a bit of exposure, which definitely is a good thing. 🙂