tagz


Nametagz JSON
Version 0.2.6 PyPI version JSON
download
home_page
Summarytagz is a html tags builder
upload_time2023-09-19 19:26:08
maintainer
docs_urlNone
authorDmitry Orlov
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Github Actions](https://github.com/mosquito/tagz/workflows/tests/badge.svg)](https://github.com/mosquito/tagz/actions?query=branch%3Amaster)
[![Coveralls](https://coveralls.io/repos/github/mosquito/tagz/badge.svg?branch=master)](https://coveralls.io/github/mosquito/tagz?branch=master)
[![Latest Version](https://img.shields.io/pypi/v/tagz.svg)](https://pypi.python.org/pypi/tagz/)
[![python wheel](https://img.shields.io/pypi/wheel/tagz.svg)](https://pypi.python.org/pypi/tagz/)
[![Python Versions](https://img.shields.io/pypi/pyversions/tagz.svg)](https://pypi.python.org/pypi/tagz/)
[![license](https://img.shields.io/pypi/l/tagz.svg)](https://pypi.python.org/pypi/tagz/)

# `tagz`

`tagz` – is an extremely simple library for building html documents without using templates, 
just with python code.

```python
from tagz import Page, StyleSheet, Style, html


page = Page(
    lang="en",
    body_element=html.body(
        html.h1("Hello"),
        html.div(
            html.strong("world"),
        ),
        html.a(
            "example link",
            html.i("with italic text"),
            href="https://example.com/"
        ),
    ),
    head_elements=(
        html.meta(charset="utf-8"),
        html.meta(name="viewport", content="width=device-width, initial-scale=1"),
        html.title("tagz example page"),
        html.link(href="/static/css/bootstrap.min.css", rel="stylesheet"),
        html.script(src="/static/js/bootstrap.bundle.min.js"),
        html.style(
            StyleSheet({
                "body": Style(padding="0", margin="0"),
                (".container", ".container-fluid"): Style(transition="opacity 600ms ease-in"),
            })
        )
    ),
)

# `pretty=False` should be faster but performs not a human-readable result
print(page.to_html5(pretty=True))
```

writes something like this:

```html
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8"/>
		<meta name="viewport" content="width=device-width, initial-scale=1"/>
		<title>
			tagz example page
		</title>
		<link href="/static/css/bootstrap.min.css" rel="stylesheet"/>
		<script src="/static/js/bootstrap.bundle.min.js">
		</script>
		<style>
			body {padding: 0; margin: 0;}
			.container, .container-fluid {transition: opacity 600ms ease-in;}
		</style>
	</head>
	<body>
		<h1>
			Hello
		</h1>
		<div>
			<strong>
				world
			</strong>
		</div>
		<a href="https://example.com/">
			example link
			<i>
				with italic text
			</i>
		</a>
	</body>
</html>
```

# Features

* Any custom tags is supported:
  ```python
  from tagz import html
  assert str(html.my_custom_tag("hello")) == "<my-custom-tag>hello</my-custom-tag>" 
  ```
* Pretty printing html
  ```python
  from tagz import html

  print(
      html.div(
         "Hello", html.strong("world"),
      ).to_string(pretty=True)
  )
  #<div>
  #	Hello
  #	<strong>
  #		world
  #	</strong>
  #</div>
  ```
* `Style` helper object:
  ```python
  from tagz import Style
  assert str(Style(color="#ffffff")) == "color: #ffffff;"
  ```
* `StyleSheet` helper object
  ```python
  from tagz import Style, StyleSheet

  # body {padding: 0;margin: 0}
  # a, div {transition: opacity 600ms ease-in}
  print(
      str(
          StyleSheet({
              "body": Style(padding="0", margin="0"),
              ("div", "a"): Style(transition="opacity 600ms ease-in"),
          })
      )
  )
  ```

# More examples

## Building page from parts

You can reuse the code, and assemble the page piece by piece, 
to do this you can modify elements already added to the tags:

```python
from tagz import html, Page

# Make an content element
content = html.div(id='content')

page = Page(
    lang="en",
    body_element=html.body(
        html.h1("Example page"),
        html.hr(),
        # Adding it to the page
        content,
    ),
    head_elements=(
        html.meta(charset="utf-8"),
        html.title("tagz partial page"),
    ),
)

content.append("Example page content")

print(page.to_html5(pretty=True))
```

This prints something like this:  

```html
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8"/>
		<title>
			tagz example page
		</title>
	</head>
	<body>
		<h1>
			Example page
		</h1>
		<hr/>
		<div id="content">
			Example page content
		</div>
	</body>
</html>
```

## Convert CSV to html table

```python
from io import StringIO
from urllib.request import urlopen
from csv import reader
from tagz import html, Page, Style

url = (
    'https://media.githubusercontent.com/media/datablist/'
    'sample-csv-files/main/files/organizations/'
    'organizations-10000.csv'
)

csv = reader(StringIO(urlopen(url).read().decode()))
table = html.table(border='1', style=Style(border_collapse="collapse"))
content = list(csv)

# Make table header 
table.append(html.tr(*map(html.th, content[0])))

# Add table rows
for csv_row in content[1:]:
    table.append(html.tr(*map(html.td, csv_row)))

page = Page(
    lang="en",
    body_element=html.body(
        html.h1("Converted CSV"),
        table,
        "Content of this page has been automatically converted from",
        html.a(url, href=url),
    ),
    head_elements=(
        html.meta(charset="utf-8"),
        html.title("tagz csv example page"),
    ),
)

with open("/tmp/csv.html", "w") as fp:
    fp.write(page.to_html5())
```
            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "tagz",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Dmitry Orlov",
    "author_email": "me@mosquito.su",
    "download_url": "https://files.pythonhosted.org/packages/9e/9b/c7633b8843c42d8b748d614803e70f6f460c4c48bcee9af97ebb578d3805/tagz-0.2.6.tar.gz",
    "platform": null,
    "description": "[![Github Actions](https://github.com/mosquito/tagz/workflows/tests/badge.svg)](https://github.com/mosquito/tagz/actions?query=branch%3Amaster)\n[![Coveralls](https://coveralls.io/repos/github/mosquito/tagz/badge.svg?branch=master)](https://coveralls.io/github/mosquito/tagz?branch=master)\n[![Latest Version](https://img.shields.io/pypi/v/tagz.svg)](https://pypi.python.org/pypi/tagz/)\n[![python wheel](https://img.shields.io/pypi/wheel/tagz.svg)](https://pypi.python.org/pypi/tagz/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/tagz.svg)](https://pypi.python.org/pypi/tagz/)\n[![license](https://img.shields.io/pypi/l/tagz.svg)](https://pypi.python.org/pypi/tagz/)\n\n# `tagz`\n\n`tagz` \u2013 is an extremely simple library for building html documents without using templates, \njust with python code.\n\n```python\nfrom tagz import Page, StyleSheet, Style, html\n\n\npage = Page(\n    lang=\"en\",\n    body_element=html.body(\n        html.h1(\"Hello\"),\n        html.div(\n            html.strong(\"world\"),\n        ),\n        html.a(\n            \"example link\",\n            html.i(\"with italic text\"),\n            href=\"https://example.com/\"\n        ),\n    ),\n    head_elements=(\n        html.meta(charset=\"utf-8\"),\n        html.meta(name=\"viewport\", content=\"width=device-width, initial-scale=1\"),\n        html.title(\"tagz example page\"),\n        html.link(href=\"/static/css/bootstrap.min.css\", rel=\"stylesheet\"),\n        html.script(src=\"/static/js/bootstrap.bundle.min.js\"),\n        html.style(\n            StyleSheet({\n                \"body\": Style(padding=\"0\", margin=\"0\"),\n                (\".container\", \".container-fluid\"): Style(transition=\"opacity 600ms ease-in\"),\n            })\n        )\n    ),\n)\n\n# `pretty=False` should be faster but performs not a human-readable result\nprint(page.to_html5(pretty=True))\n```\n\nwrites something like this:\n\n```html\n<!doctype html>\n<html lang=\"en\">\n\t<head>\n\t\t<meta charset=\"utf-8\"/>\n\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n\t\t<title>\n\t\t\ttagz example page\n\t\t</title>\n\t\t<link href=\"/static/css/bootstrap.min.css\" rel=\"stylesheet\"/>\n\t\t<script src=\"/static/js/bootstrap.bundle.min.js\">\n\t\t</script>\n\t\t<style>\n\t\t\tbody {padding: 0; margin: 0;}\n\t\t\t.container, .container-fluid {transition: opacity 600ms ease-in;}\n\t\t</style>\n\t</head>\n\t<body>\n\t\t<h1>\n\t\t\tHello\n\t\t</h1>\n\t\t<div>\n\t\t\t<strong>\n\t\t\t\tworld\n\t\t\t</strong>\n\t\t</div>\n\t\t<a href=\"https://example.com/\">\n\t\t\texample link\n\t\t\t<i>\n\t\t\t\twith italic text\n\t\t\t</i>\n\t\t</a>\n\t</body>\n</html>\n```\n\n# Features\n\n* Any custom tags is supported:\n  ```python\n  from tagz import html\n  assert str(html.my_custom_tag(\"hello\")) == \"<my-custom-tag>hello</my-custom-tag>\" \n  ```\n* Pretty printing html\n  ```python\n  from tagz import html\n\n  print(\n      html.div(\n         \"Hello\", html.strong(\"world\"),\n      ).to_string(pretty=True)\n  )\n  #<div>\n  #\tHello\n  #\t<strong>\n  #\t\tworld\n  #\t</strong>\n  #</div>\n  ```\n* `Style` helper object:\n  ```python\n  from tagz import Style\n  assert str(Style(color=\"#ffffff\")) == \"color: #ffffff;\"\n  ```\n* `StyleSheet` helper object\n  ```python\n  from tagz import Style, StyleSheet\n\n  # body {padding: 0;margin: 0}\n  # a, div {transition: opacity 600ms ease-in}\n  print(\n      str(\n          StyleSheet({\n              \"body\": Style(padding=\"0\", margin=\"0\"),\n              (\"div\", \"a\"): Style(transition=\"opacity 600ms ease-in\"),\n          })\n      )\n  )\n  ```\n\n# More examples\n\n## Building page from parts\n\nYou can reuse the code, and assemble the page piece by piece, \nto do this you can modify elements already added to the tags:\n\n```python\nfrom tagz import html, Page\n\n# Make an content element\ncontent = html.div(id='content')\n\npage = Page(\n    lang=\"en\",\n    body_element=html.body(\n        html.h1(\"Example page\"),\n        html.hr(),\n        # Adding it to the page\n        content,\n    ),\n    head_elements=(\n        html.meta(charset=\"utf-8\"),\n        html.title(\"tagz partial page\"),\n    ),\n)\n\ncontent.append(\"Example page content\")\n\nprint(page.to_html5(pretty=True))\n```\n\nThis prints something like this:  \n\n```html\n<!doctype html>\n<html lang=\"en\">\n\t<head>\n\t\t<meta charset=\"utf-8\"/>\n\t\t<title>\n\t\t\ttagz example page\n\t\t</title>\n\t</head>\n\t<body>\n\t\t<h1>\n\t\t\tExample page\n\t\t</h1>\n\t\t<hr/>\n\t\t<div id=\"content\">\n\t\t\tExample page content\n\t\t</div>\n\t</body>\n</html>\n```\n\n## Convert CSV to html table\n\n```python\nfrom io import StringIO\nfrom urllib.request import urlopen\nfrom csv import reader\nfrom tagz import html, Page, Style\n\nurl = (\n    'https://media.githubusercontent.com/media/datablist/'\n    'sample-csv-files/main/files/organizations/'\n    'organizations-10000.csv'\n)\n\ncsv = reader(StringIO(urlopen(url).read().decode()))\ntable = html.table(border='1', style=Style(border_collapse=\"collapse\"))\ncontent = list(csv)\n\n# Make table header \ntable.append(html.tr(*map(html.th, content[0])))\n\n# Add table rows\nfor csv_row in content[1:]:\n    table.append(html.tr(*map(html.td, csv_row)))\n\npage = Page(\n    lang=\"en\",\n    body_element=html.body(\n        html.h1(\"Converted CSV\"),\n        table,\n        \"Content of this page has been automatically converted from\",\n        html.a(url, href=url),\n    ),\n    head_elements=(\n        html.meta(charset=\"utf-8\"),\n        html.title(\"tagz csv example page\"),\n    ),\n)\n\nwith open(\"/tmp/csv.html\", \"w\") as fp:\n    fp.write(page.to_html5())\n```",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "tagz is a html tags builder",
    "version": "0.2.6",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "94f78c63e640508c9d4f884910470bf68c8fbe8b35774f131a101d00244f4b3d",
                "md5": "654e9b7013edc6390fc30463011e1f62",
                "sha256": "26f05a750b31bca2baf350121ec9f69552d066b5f1d6c8a8ddefb19902135837"
            },
            "downloads": -1,
            "filename": "tagz-0.2.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "654e9b7013edc6390fc30463011e1f62",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 5429,
            "upload_time": "2023-09-19T19:26:07",
            "upload_time_iso_8601": "2023-09-19T19:26:07.138343Z",
            "url": "https://files.pythonhosted.org/packages/94/f7/8c63e640508c9d4f884910470bf68c8fbe8b35774f131a101d00244f4b3d/tagz-0.2.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9e9bc7633b8843c42d8b748d614803e70f6f460c4c48bcee9af97ebb578d3805",
                "md5": "1092a38dc15675ca9efeabcbe788aedb",
                "sha256": "05542f92c1b735d7c0953d25ebf7ee8a95f5ab8c53ee6ed5082ab598dbc82ce1"
            },
            "downloads": -1,
            "filename": "tagz-0.2.6.tar.gz",
            "has_sig": false,
            "md5_digest": "1092a38dc15675ca9efeabcbe788aedb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 5245,
            "upload_time": "2023-09-19T19:26:08",
            "upload_time_iso_8601": "2023-09-19T19:26:08.948027Z",
            "url": "https://files.pythonhosted.org/packages/9e/9b/c7633b8843c42d8b748d614803e70f6f460c4c48bcee9af97ebb578d3805/tagz-0.2.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-19 19:26:08",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "tagz"
}
        
Elapsed time: 0.11691s