virtualenv

virtualenv is a tool to create isolated Python environments.

Install from source

Setup virtualenv with python3 in Debian

   1 cd ~/tmp
   2 mkdir projectx
   3 cd projectx
   4 sudo apt install python3-venv
   5 python3 -m venv virtenv
   6 . virtenv/bin/activate
   7 pip install cherrypy routes jinja2
   8 find .
   9 nano jinja2test.py
  10 mkdir templates
  11 nano templates/test.html 
  12 python3 jinja2test.py

jinja2test.py

   1 import sys
   2 sys.stdout = sys.stderr
   3 import cherrypy
   4 
   5 from jinja2 import Environment, FileSystemLoader
   6 env = Environment(loader=FileSystemLoader('/home/vitor/tmp/projectx/templates'))
   7 
   8 cherrypy.config.update({'environment': 'embedded'})
   9 
  10 class Jinja2Test(object):
  11     @cherrypy.expose
  12     def index(self):
  13         return "Hello World Jinja2Test!!!!"
  14     @cherrypy.expose
  15     def add(self,param1,param2):
  16        return str( int(param1)+int(param2) )
  17     @cherrypy.expose
  18     def testpage(self):
  19         t = env.get_template('test.html')
  20         navItems=[]
  21         for i in range(1,10):
  22                 navItems.append( {'href':'hrefx','caption':'cap %d'%(i) } )
  23         return t.render( titlex="Titleeeee" , navigation=navItems )
  24 
  25 jjtest = Jinja2Test()
  26 cherrypy.server.socket_host = '0.0.0.0'
  27 cherrypy.quickstart(jjtest)

templates/test.html

   1 <html>
   2     <head></head>
   3     <body>
   4         <ul id="navigation">
   5         {% if navigation|count > 0 %}
   6         {% for item in navigation %}
   7         <li>
   8           <a href="{{item.href}}">{{item.caption}}</a> 
   9         </li>
  10         {% endfor %}
  11         {% endif %}
  12         </ul>
  13         <h1>My webpage {{titlex}} </h1>
  14     </body>
  15 </html>

Python/virtualenv (last edited 2021-06-18 14:38:35 by localhost)