METADATA 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. Metadata-Version: 2.0
  2. Name: dominate
  3. Version: 2.3.1
  4. Summary: Dominate is a Python library for creating and manipulating HTML documents using an elegant DOM API.
  5. Home-page: http://github.com/Knio/dominate/
  6. Author: Tom Flanagan and Jake Wharton
  7. Author-email: tom@zkpq.ca
  8. License: LICENSE.txt
  9. Keywords: framework templating template html xhtml python html5
  10. Platform: UNKNOWN
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
  13. Classifier: Operating System :: OS Independent
  14. Classifier: Programming Language :: Python
  15. Classifier: Programming Language :: Python :: 2
  16. Classifier: Programming Language :: Python :: 2.7
  17. Classifier: Programming Language :: Python :: 3
  18. Classifier: Programming Language :: Python :: 3.3
  19. Classifier: Programming Language :: Python :: 3.4
  20. Classifier: Programming Language :: Python :: 3.5
  21. Classifier: Programming Language :: Python :: Implementation :: PyPy
  22. Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
  23. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  24. Classifier: Topic :: Text Processing :: Markup :: HTML
  25. Dominate
  26. ========
  27. `Dominate` is a Python library for creating and manipulating HTML documents using an elegant DOM API.
  28. It allows you to write HTML pages in pure Python very concisely, which eliminates the need to learn another template language, and lets you take advantage of the more powerful features of Python.
  29. Python:
  30. ```python
  31. import dominate
  32. from dominate.tags import *
  33. doc = dominate.document(title='Dominate your HTML')
  34. with doc.head:
  35. link(rel='stylesheet', href='style.css')
  36. script(type='text/javascript', src='script.js')
  37. with doc:
  38. with div(id='header').add(ol()):
  39. for i in ['home', 'about', 'contact']:
  40. li(a(i.title(), href='/%s.html' % i))
  41. with div():
  42. attr(cls='body')
  43. p('Lorem ipsum..')
  44. print(doc)
  45. ```
  46. Output:
  47. ```html
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <title>Dominate your HTML</title>
  52. <link href="style.css" rel="stylesheet">
  53. <script src="script.js" type="text/javascript"></script>
  54. </head>
  55. <body>
  56. <div id="header">
  57. <ol>
  58. <li>
  59. <a href="/home.html">Home</a>
  60. </li>
  61. <li>
  62. <a href="/about.html">About</a>
  63. </li>
  64. <li>
  65. <a href="/contact.html">Contact</a>
  66. </li>
  67. </ol>
  68. </div>
  69. <div class="body">
  70. <p>Lorem ipsum..</p>
  71. </div>
  72. </body>
  73. </html>
  74. ```
  75. Compatibility
  76. -------------
  77. `Dominate` is compatible with both Python 2.7 and Python 3.3. There are known issues with Python 3.2 and below.
  78. [![Build Status](https://travis-ci.org/Knio/dominate.png?branch=master)](https://travis-ci.org/Knio/dominate)
  79. [![Coverage Status](https://coveralls.io/repos/Knio/dominate/badge.png?branch=master)](https://coveralls.io/r/Knio/dominate?branch=master)
  80. Installation
  81. ------------
  82. The recommended way to install `dominate` is with
  83. [`pip`](http://pypi.python.org/pypi/pip/):
  84. sudo pip install dominate
  85. [![PyPI version](https://badge.fury.io/py/dominate.png)](http://badge.fury.io/py/dominate)
  86. Developed By
  87. ------------
  88. * Tom Flanagan - <tom@zkpq.ca>
  89. * Jake Wharton - <jakewharton@gmail.com>
  90. * [Brad Janke](//github.com/bradj)
  91. Git repository located at
  92. [github.com/Knio/dominate](//github.com/Knio/dominate)
  93. Examples
  94. ========
  95. All examples assume you have imported the appropriate tags or entire tag set:
  96. ```python
  97. from dominate.tags import *
  98. ```
  99. Hello, World!
  100. -------------
  101. The most basic feature of `dominate` exposes a class for each HTML element, where the constructor
  102. accepts child elements, text, or keyword attributes. `dominate` nodes return their HTML representation
  103. from the `__str__`, `__unicode__`, and `render()` methods.
  104. ```python
  105. print(html(body(h1('Hello, World!'))))
  106. ```
  107. ```html
  108. <html>
  109. <body>
  110. <h1>Hello, World!</h1>
  111. </body>
  112. </html>
  113. ```
  114. Attributes
  115. ----------
  116. `Dominate` can also use keyword arguments to append attributes onto your tags. Most of the attributes are a direct copy from the HTML spec with a few variations.
  117. For attributes `class` and `for` which conflict with Python's [reserved keywords](http://docs.python.org/2/reference/lexical_analysis.html#keywords "Reserved Keywords"), you can use the following aliases:
  118. | class | for |
  119. |-------|-----|
  120. |_class | _for |
  121. |cls | fr |
  122. |className|htmlFor|
  123. |class_name|html_for|
  124. ```python
  125. test = label(cls='classname anothername', fr='someinput')
  126. print(test)
  127. ```
  128. ```html
  129. <label class="classname anothername" for="someinput"></label>
  130. ```
  131. Use `data_*` for [custom HTML5 data attributes](http://www.w3.org/html/wg/drafts/html/master/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes "HTML5 Data Attributes").
  132. ```python
  133. test = div(data_employee='101011')
  134. print(test)
  135. ```
  136. ```html
  137. <div data-employee="101011"></div>
  138. ```
  139. You can also modify the attributes of tags through a dictionary-like interface:
  140. ```python
  141. header = div()
  142. header['id'] = 'header'
  143. print(header)
  144. ```
  145. ```html
  146. <div id="header"></div>
  147. ```
  148. Complex Structures
  149. ------------------
  150. Through the use of the `+=` operator and the `.add()` method you can easily create more advanced structures.
  151. Create a simple list:
  152. ```python
  153. list = ul()
  154. for item in range(4):
  155. list += li('Item #', item)
  156. print(list)
  157. ```
  158. ```html
  159. <ul>
  160. <li>Item #0</li>
  161. <li>Item #1</li>
  162. <li>Item #2</li>
  163. <li>Item #3</li>
  164. </ul>
  165. ```
  166. `dominate` supports iterables to help streamline your code:
  167. ```python
  168. print(ul(li(a(name, href=link), __pretty=False) for name, link in menu_items))
  169. ```
  170. ```html
  171. <ul>
  172. <li><a href="/home/">Home</a></li>
  173. <li><a href="/about/">About</a></li>
  174. <li><a href="/downloads/">Downloads</a></li>
  175. <li><a href="/links/">Links</a></li>
  176. </ul>
  177. ```
  178. A simple document tree:
  179. ```python
  180. _html = html()
  181. _body = _html.add(body())
  182. header = _body.add(div(id='header'))
  183. content = _body.add(div(id='content'))
  184. footer = _body.add(div(id='footer'))
  185. print(_html)
  186. ```
  187. ```html
  188. <html>
  189. <body>
  190. <div id="header"></div>
  191. <div id="content"></div>
  192. <div id="footer"></div>
  193. </body>
  194. </html>
  195. ```
  196. For clean code, the `.add()` method returns children in tuples. The above example can be cleaned up and expanded like this:
  197. ```python
  198. _html = html()
  199. _head, _body = _html.add(head(title('Simple Document Tree')), body())
  200. names = ['header', 'content', 'footer']
  201. header, content, footer = _body.add(div(id=name) for name in names)
  202. print(_html)
  203. ```
  204. ```html
  205. <html>
  206. <head>
  207. <title>Simple Document Tree</title>
  208. </head>
  209. <body>
  210. <div id="header"></div>
  211. <div id="content"></div>
  212. <div id="footer"></div>
  213. </body>
  214. </html>
  215. ```
  216. You can modify the attributes of tags through a dictionary-like interface:
  217. ```python
  218. header = div()
  219. header['id'] = 'header'
  220. print(header)
  221. ```
  222. ```html
  223. <div id="header"></div>
  224. ```
  225. Or the children of a tag though an array-line interface:
  226. ```python
  227. header = div('Test')
  228. header[0] = 'Hello World'
  229. print(header)
  230. ```
  231. ```html
  232. <div>Hello World</div>
  233. ```
  234. Comments can be created using objects too!
  235. ```python
  236. print(comment('BEGIN HEADER'))
  237. ```
  238. ```html
  239. <!--BEGIN HEADER-->
  240. ```
  241. ```python
  242. print(comment(p('Upgrade to newer IE!'), condition='lt IE9'))
  243. ```
  244. ```html
  245. <!--[if lt IE9]>
  246. <p>Upgrade to newer IE!</p>
  247. <![endif]-->
  248. ```
  249. Rendering
  250. ---------
  251. By default, `render()` tries to make all output human readable, with one HTML
  252. element per line and two spaces of indentation.
  253. This behavior can be controlled by the `__pretty` (default: `True` except for
  254. certain element types like `pre`) attribute when creating an element, and by
  255. the `pretty` (default: `True`), `indent` (default: ` `) and `xhtml` (default: `False`)
  256. arguments to `render()`. Rendering options propagate to all descendant nodes.
  257. ```python
  258. a = div(span('Hello World'))
  259. print(a.render())
  260. ```
  261. ```html
  262. <div>
  263. <span>Hello World</span>
  264. </div>
  265. ```
  266. ```python
  267. print(a.render(pretty=False))
  268. ```
  269. ```html
  270. <div><span>Hello World</span></div>
  271. ```
  272. ```python
  273. print(a.render(indent='\t'))
  274. ```
  275. ```html
  276. <div>
  277. <span>Hello World</span>
  278. </div>
  279. ```
  280. ```python
  281. a = div(span('Hello World'), __pretty=False)
  282. print(a.render())
  283. ```
  284. ```html
  285. <div><span>Hello World</span></div>
  286. ```
  287. ```python
  288. d = div()
  289. with d:
  290. hr()
  291. p("Test")
  292. br()
  293. print(d.render())
  294. print(d.render(xhtml=True))
  295. ```
  296. ```html
  297. <div>
  298. <hr>
  299. <p>Test</p><br>
  300. </div>
  301. <div>
  302. <hr />
  303. <p>Test</p><br />
  304. </div>
  305. ```
  306. Context Managers
  307. ----------------
  308. You can also add child elements using Python's `with` statement:
  309. ```python
  310. h = ul()
  311. with h:
  312. li('One')
  313. li('Two')
  314. li('Three')
  315. print(h)
  316. ```
  317. ```html
  318. <ul>
  319. <li>One</li>
  320. <li>Two</li>
  321. <li>Three</li>
  322. </ul>
  323. ```
  324. You can use this along with the other mechanisms of adding children elements, including nesting `with` statements, and it works as expected:
  325. ```python
  326. h = html()
  327. with h.add(body()).add(div(id='content')):
  328. h1('Hello World!')
  329. p('Lorem ipsum ...')
  330. with table().add(tbody()):
  331. l = tr()
  332. l += td('One')
  333. l.add(td('Two'))
  334. with l:
  335. td('Three')
  336. print(h)
  337. ```
  338. ```html
  339. <html>
  340. <body>
  341. <div id="content">
  342. <h1>Hello World!</h1>
  343. <p>Lorem ipsum ...</p>
  344. <table>
  345. <tbody>
  346. <tr>
  347. <td>One</td>
  348. <td>Two</td>
  349. <td>Three</td>
  350. </tr>
  351. </tbody>
  352. </table>
  353. </div>
  354. </body>
  355. </html>
  356. ```
  357. When the context is closed, any nodes that were not already added to something get added to the current context.
  358. Attributes can be added to the current context with the `attr` function:
  359. ```python
  360. d = div()
  361. with d:
  362. attr(id='header')
  363. print(d)
  364. ```
  365. ```html
  366. <div id="header"></div>
  367. ```
  368. Decorators
  369. ----------
  370. `Dominate` is great for creating reusable widgets for parts of your page. Consider this example:
  371. ```python
  372. def greeting(name):
  373. with div() as d:
  374. p('Hello, %s' % name)
  375. return d
  376. print(greeting('Bob'))
  377. ```
  378. ```html
  379. <div>
  380. <p>Hello, Bob</p>
  381. </div>
  382. ```
  383. You can see the following pattern being repeated here:
  384. ```python
  385. def widget(parameters):
  386. with tag() as t:
  387. ...
  388. return t
  389. ```
  390. This boilerplate can be avoided by using tags (objects and instances) as decorators
  391. ```python
  392. @div
  393. def greeting(name):
  394. p('Hello %s' % name)
  395. print(greeting('Bob'))
  396. ```
  397. ```html
  398. <div>
  399. <p>Hello Bob</p>
  400. </div>
  401. ```
  402. The decorated function will return a new instance of the tag used to decorate it, and execute in a `with` context which will collect all the nodes created inside it.
  403. You can also use instances of tags as decorators, if you need to add attributes or other data to the root node of the widget.
  404. Each call to the decorated function will return a copy of the node used to decorate it.
  405. ```python
  406. @div(h2('Welcome'), cls='greeting')
  407. def greeting(name):
  408. p('Hello %s' % name)
  409. print(greeting('Bob'))
  410. ```
  411. ```html
  412. <div class="greeting">
  413. <h2>Welcome</h2>
  414. <p>Hello Bob</p>
  415. </div>
  416. ```
  417. Creating Documents
  418. ------------------
  419. Since creating the common structure of an HTML document everytime would be excessively tedious dominate provides a class to create and manage them for you: `document`.
  420. When you create a new document, the basic HTML tag structure is created for you.
  421. ```python
  422. d = document()
  423. print(d)
  424. ```
  425. ```html
  426. <!DOCTYPE html>
  427. <html>
  428. <head>
  429. <title>Dominate</title>
  430. </head>
  431. <body></body>
  432. </html>
  433. ```
  434. The `document` class accepts `title`, `doctype`, and `request` keyword arguments.
  435. The default values for these arguments are `Dominate`, `<!DOCTYPE html>`, and `None` respectively.
  436. The `document` class also provides helpers to allow you to access the `html`, `head`, and `body` nodes directly.
  437. ```python
  438. d = document()
  439. ```
  440. ```python
  441. >>> d.html
  442. <dominate.tags.html: 0 attributes, 2 children>
  443. >>> d.head
  444. <dominate.tags.head: 0 attributes, 0 children>
  445. >>> d.body
  446. <dominate.tags.body: 0 attributes, 0 children>
  447. ```
  448. You should notice that here the `head` tag contains zero children.
  449. This is because the default `title` tag is only added when the document is rendered and the `head` element does not explicitly contain one.
  450. The `document` class also provides helpers to allow you to directly add nodes to the `body` tag.
  451. ```python
  452. d = document()
  453. d += h1('Hello, World!')
  454. d += p('This is a paragraph.')
  455. print(d)
  456. ```
  457. ```html
  458. <!DOCTYPE html>
  459. <html>
  460. <head>
  461. <title>Dominate</title>
  462. </head>
  463. <body>
  464. <h1>Hello, World!</h1>
  465. <p>This is a paragraph.</p>
  466. </body>
  467. </html>
  468. ```