{"id":540,"date":"2014-02-10T05:15:38","date_gmt":"2014-02-10T13:15:38","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=540"},"modified":"2014-02-10T05:15:38","modified_gmt":"2014-02-10T13:15:38","slug":"webpy-example","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/webpy-example\/","title":{"rendered":"webpy Example"},"content":{"rendered":"

A short write up on how to bootstrap a webpy app and at the same time show how to make a system call with popen. \u00a0I based this short app on the skeleton example here:\u00a0http:\/\/webpy.org\/skeleton\/0.3<\/p>\n

Just a note on where my test live for my own reference later.<\/p>\n

\r\n$ pwd\r\n\/DATA\/src\/hvInfo\r\n\r\n$ ls\r\ncall1.py call.pyc config.pyc hvInfo.pyc my test with spaces_ and more... templates view.pyc web.py-0.37\r\ncall.py config.py hvInfo.py index.html simpleHTTP.py view.py web web.py-0.37.tar\r\n<\/pre>\n

Main python app:<\/span><\/strong><\/p>\n

hvInfo.py<\/em><\/p>\n

\r\nimport web\r\nimport view, config\r\nfrom view import render\r\n\r\nurls = (\r\n  '\/', 'index'\r\n)\r\n\r\nclass index:\r\n  def GET(self):\r\n    return render.base(view.listing())\r\n\r\nif __name__ == "__main__": \r\n  app = web.application(urls, globals())\r\n  app.run()\r\n<\/pre>\n

Define some functions here:<\/strong><\/span><\/p>\n

call.py<\/em><\/p>\n

\r\nimport config\r\n#from subprocess import Popen, PIPE\r\nimport subprocess\r\n\r\ndef listing_experiment1():\r\n    cmd = "ls -l ~\/"\r\n    p = Popen(cmd , shell=True, stdout=PIPE, stderr=PIPE)\r\n    #out, err = p.communicate()\r\n    #print "Return code: ", p.returncode\r\n    #return out.rstrip(), err.rstrip()\r\n    outputlines = filter(lambda x:len(x)>0,(line.strip() for line in p.stdout))\r\n    return outputlines\r\n    #return out.replace('\\n','<br>')\r\n\r\n    ##return config.DB.select('items', **k)\r\n\r\ndef dir_listing():\r\n  ## http:\/\/stackoverflow.com\/questions\/8880461\/python-subprocess-output-to-list-or-file\r\n  ls_lines = subprocess.check_output(['ls', '-l']).splitlines()\r\n  ## I want to pass output as separate fields but space as a delimiter won't do the trick since filenames can have spaces...\r\n  ls_arr= []\r\n  for item in ls_lines:\r\n    if not "total" in item:\r\n      f = item.split()\r\n      #fname = item.join(str(v) for v in item.index if v > 7)\r\n      fname = ""\r\n      for idx,val in enumerate(f):\r\n        if idx > 7:\r\n          fname = fname + str(val) + " "\r\n      fname = fname[:-1]\r\n      ls_fields = [f[0],f[1],f[2],f[3],f[4],f[5]+"-"+f[6]+"-"+f[7],fname]\r\n      ls_arr.append(ls_fields)\r\n  return ls_arr\r\n<\/pre>\n

Lets maintain a config file. Especially when DB access becomes necessary.<\/span><\/strong><\/p>\n

config.py<\/em><\/p>\n

\r\nimport web\r\n#DB = web.database(dbn='postgres', db='appname', user='username', pw='')\r\ncache = False\r\n<\/pre>\n

Following MVC type framework. \u00a0It makes sense to keep presentation separate.<\/strong><\/span><\/p>\n

view.py<\/em><\/p>\n

\r\nimport web\r\nimport call\r\nimport config\r\n\r\nt_globals = dict(\r\n  datestr=web.datestr,\r\n)\r\nrender = web.template.render('templates\/', cache=config.cache, \r\n    globals=t_globals)\r\nrender._keywords['globals']['render'] = render\r\n\r\ndef listing():\r\n    l = call.dir_listing()\r\n    return render.listing(l)\r\n<\/pre>\n

Templates as follow:<\/span><\/strong><\/p>\n

\r\n$ cat templates\/base.html\r\n<\/pre>\n
\r\n$def with (page, title=None)\r\n<html><head>\r\n<title>hvInfo\\\r\n$if title: : $title\\\r\n<\/title>\r\n<\/head><body>\r\n<h1><a href="\/">hvInfo<\/a><\/h1>\r\n$:page   \r\n<\/body><\/html>\r\n<\/pre>\n
\r\n$ cat templates\/listing.html\r\n<\/pre>\n
\r\n$def with (items)\r\n  <table>\r\n  $for item in items:\r\n    <tr><td>$item[0]<td>$item[1]<td>$item[2]<td>$item[3]<td>$item[4]<td>$item[5]<td><b>$item[6]<\/b>\r\n  <\/table>\r\n<\/pre>\n
\r\n$ cat templates\/item.html\r\n<\/pre>\n
\r\n$def with (item)\r\n\r\n$item\r\n<\/pre>\n

Run web server as follow:<\/strong><\/span><\/p>\n

\r\n$ python hvInfo.py\r\nhttp:\/\/0.0.0.0:8080\/\r\n<\/pre>\n

Test in browser at this link: http:\/\/localhost:8080\/<\/p>\n","protected":false},"excerpt":{"rendered":"

A short write up on how to bootstrap a webpy app and at the same time show how to make<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/540"}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}