Posts

Showing posts from January, 2019

Python HTML Generator using Yattag Part 2

Python HTML Generator 5. Shortcut for nodes that contain only text If you're producing HTML or XML, you've probably realized that most tag nodes contain only text. In order to write these in a terser way, use the  line  method. doc, tag, text, line=Doc().ttl() with tag('ul', id='course'): line('li', 'BCA', klass="degree") line('li', 'BBA') line('li', 'BSW') You'll get: <ul id="course"> <li class="degree">BCA</li> <li>BBA</li> <li>BSW</li> </ul> Oh, I forgot to introduce the  ttl  method I just used in the example above. It works just like the  tagtext  method I've talked about in the beginning of the tutorial, but instead of returning the triplet  (doc, doc.tag, doc.text) , it returns the quadruplet  (doc, doc.tag, doc.text, doc.line) . 6. Indentation We've added indentation in the

Python HTML Generator using Yattag Part 1

Python HTML Generator Download and install yattag pip install yattag You can download the archive here: https://pypi.python.org/pypi/yattag                python setup.py install      or:         python3 setup.py install Yattag is a Python library for generating HTML or XML in a pythonic way. With Yattag, ·         you don't have to worry about closing HTML tags ·         your HTML templates are Python code. Not a weird template language. Just Python. ·         you can easily render HTML forms, with defaults values and error messages. It's actually easier and more readable to generate dynamic HTML with Yattag than to write static HTML. 1. Tags and text from yattag import Doc doc,tag,text=Doc().tagtext() with tag('h1'):           text('Hello World!') print(doc.getvalue()) How this works Have you ever used a list of strings and used the  join  method to produce just one big string? I mean, just like this: mylist = [

Python OOP Part 2

In Python, we use double underscore (Or __) before the attributes name and those attributes will not be directly visible outside. We can access the value of hidden attribute by a tricky syntax. Private methods are accessible outside their class, just not easily accessible.  Nothing in Python is truly private; internally , the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names. Printing objects gives us information about objects we are working with. In C++, we can do this by adding a friend ostream& operator << (ostream&, const Foobar&) method for the class. In Java, we use toString() method. In python this can be achieved by using __repr__ or __str__ methods. class Test:           def __init__(self, a, b):                    self.a = a                    self.b = b           def __repr__(self):                    return "Test a:%s b:%s" % (self.a, self.b)