Rattle.pyのドキュメントへようこそ!

rattlepyモジュール : Rattle.py APIリファレンス

Rattle.py - A Pure Python Templating Library for HTML A pure python templating library for html. Rattle.py has no special notation like Django or Jinja. For example:

<html>
    <head>
        <title>Hello, PTL!</title>
    </head>
    <body>
        <h1 class="heading">Hello, PTL!</h1>
    </body>
</html>

The above HTML equals to below Python code with rattle.py:

greeting = "Hello, PTL!"
with html() as html:
  with head():
    with title():
      text(greeting)
  with body():
    with node("h1", className="heading"):
      text(greeting)

# show as HTML
print(html)

And then, you can also make reusable components by yourself:

def greet(name):
  with node("div", className="greet-wrapper") as component:
    with node("h1"):
      text(f"Hello, {name}=san")
    with node("button", className="ok-btn"):
      text("ok!")
  return component

# and using:
with greet("User"): pass

Enjoy!

rattlepy.elementsモジュール : Rattle.py APIリファレンス

  • 次のドキュメント: Utils
  • 前のドキュメント: Rattlepy

HTML element short-handing functions

rattlepy.elements.a(**kwargs)

リンクを作成して返します。 return Element("a", attributes...) と同じです。

rattlepy.elements.article(**kwargs)

articleノードを作成して返します。 return Element("article", attributes... ) と同じです。

rattlepy.elements.body(**kwargs)

bodyノードを作成して返します。 return Element("body", attributes...) と同じです。

rattlepy.elements.div(**kwargs)

divノードを作成して返します。 return Element("div", attributes...) と同じです。

rattlepy.elements.footer(**kwargs)

footerノードを作成して返します。 return Element("footer", attributes...) と同じです。

rattlepy.elements.h1(**kwargs)

h1ノードを作成して返します。 return Element("h1", attributes...) と同じです。

rattlepy.elements.h2(**kwargs)

h2ノードを作成して返します。 return Element("h2", attributes...) と同じです。

rattlepy.elements.h3(**kwargs)

h3ノードを作成して返します。 return Element("h3", attributes...) と同じです。

rattlepy.elements.h4(**kwargs)

h4ノードを作成して返します。 return Element("h4", attributes...) と同じです。

rattlepy.elements.h5(**kwargs)

h5ノードを作成して返します。 return Element("h5", attributes...) と同じです。

rattlepy.elements.h6(**kwargs)

h6ノードを作成して返します。 return Element("h6", attributes...) と同じです。

rattlepy.elements.head(**kwargs)

headノードを作成して返します。 return Element("head", attributes...) と同じです。

rattlepy.elements.header(**kwargs)

headerノードを作成して返します。 return Element("header", attributes...) と同じです。

rattlepy.elements.hr(**kwargs)

hrノードを作成して返します。 return Element("hr", attributes...) と同じです。

rattlepy.elements.html(**kwargs)

htmlノードを作成して返します。 return Element("html", attributes...) と同じです。

rattlepy.elements.img(**kwargs)

imgノードを作成して返します。 return Element("img", attributes...) と同じです。

rattlepy.elements.li(**kwargs)

liノードを作成して返します。 return Element("li", attributes...) と同じです。

linkノードを作成して返します。 return Element("link", attributes...) と同じです。

rattlepy.elements.main(**kwargs)

mainノードを作成して返します。 return Element("main", attributes...) と同じです。

rattlepy.elements.meta(**kwargs)

metaノードを作成して返します。 return Element("meta", attributes...) と同じです。

rattlepy.elements.ol(**kwargs)

olノードを作成して返します。 return Element("ol", attributes...) と同じです。

rattlepy.elements.p(**kwargs)

paragraphノードを作成して返します。 return Element("p", attributes...) と同じです。

rattlepy.elements.script(**kwargs)

scriptノードを作成して返します。 return Element("script", attributes...) と同じです。

rattlepy.elements.span(**kwargs)

spanノードを作成して返します。 return Element("span", attributes...) と同じです。

rattlepy.elements.style(**kwargs)

styleノードを作成して返します。 return Element("style", attributes...) と同じです。

rattlepy.elements.title(**kwargs)

titleノードを作成して返します。 return Element("title", attributes...) と同じです。

rattlepy.elements.ul(**kwargs)

ulノードを作成して返します。 return Element("ul", attributes...) と同じです。

rattlepy.elements.setTitle(string)

ドキュメントのタイトルを設定します。

この関数は以下のコードと同等です:

with title():
  text(string)

この関数はwith文の中で使う必要があります:

with setTitle("HogeHoge Page"): pass
  • 次のドキュメント: Utils
  • 前のドキュメント: Rattlepy

rattlepy.templatingモジュール : Rattle.py APIリファレンス

テンプレートクラスと関数

rattlepy.templating.escapeHtmlEntities(string)

特定の文字をエスケープする

class rattlepy.templating.Element(tag, *, className=None, **kwargs)

子ノードを持つ要素のクラス

使い方:

with Element(tagname, attributes...):
  ...

クラス属性には”class”の代わりに”className”を使ってください。 または、こういう方法もあります:

with Element(tagname, **{'class': 'my-class'}):
  ...

Pythonの”data-“のような識別子として無効な属性は、上記の方法を使って指定できます。

exposes(element=None)

カスタムコンポーネントの作成を容易にするヘルパー関数です。 動的に親ノードを変更します。

コード例:

with Element("hoge") as hoge:
  # この要素の親クラスは :code:`hoge` です。
  with Element("some-inner") as inner:
    hoge.exposes(inner)

with hoge:
  # この要素の親クラスは :code:`some-inner` です。
  with Element("other-element"):
    ...
  hoge.exposes()

with hoge:
  # この要素の親クラスは :code:`hoge` です。
  with Element("some-other-element"):
    ...
serialize(formatter='human_friendly', force_add_doctype=False)

HTML要素をシリアライズします。 str(elem) は圧縮されますが、圧縮せずに出力することも可能です。

formatter引数は[“human_friendly”, “minify”]のうちのどれかを指定してください。デフォルトは”human_friendly”です。 force_add_doctype引数にtrueを指定すると要素のタイプにかかわらず、doctype宣言を先頭に追加します。

class rattlepy.templating.SelfClosedElement(tag, *, _outer=2, className=None, **kwargs)

子ノードを持たない要素のクラス

使い方:

with Element("hoge"):
  SelfClosedElement(tagname, attributes...)
addself(*, outer=1)

特定の親ノードに自分自身を追加します。

使い方:

with some_parent_node:
  # これはsome_parent_nodeの子ノードになります。
  SelfClosedElement('hr').addself()
rattlepy.templating.text(content)

テキストノードを作成して追加します。

content引数の型はstrである必要があります。

複数行にわたる文字列は次のようにしてください:

with Element("hoge"):
  text('''\
  |some
  |multiline
  |text''')

| の前の文字列は無視されます。存在しない場合はその行のすべての文字が挿入されます。

& のようなHTMLの特殊文字はエスケープされます。 エスケープなしで挿入したい場合は rtext 関数を代わりに使ってください。

rattlepy.templating.node(tag, **kwargs)

要素を作成して返します。 Element(tag, attributes...) と同じです。

rattlepy.templating.closed(tag, **kwargs)

子ノードを持たない要素を作成して返します。 SelfClosedElement(tag, attributes...) と同じです。

rattlepy.utilsモジュール : Rattle.py APIリファレンス

Utility functions for making html more easily.

rattlepy.utils.createHeader(title, *metas)

Create head element. Usage:

with createHeader(
  "Page Title",
  {"charset": "utf-8"}):
    ...

This function equals to the code:

with head():
  for m in [{"charset": "utf-8"}]:
    meta(**m)
  setTitle("Page Title")
rattlepy.utils.scaffold(header: rattlepy.templating.Element)

Create html scaffold. This feature is under experimental.

Module rattlepy.environment : Rattle.py API Reference

Placeholder variables implementation

class rattlepy.environment.Environment

Holding values to replace placeholders. Threads have a separate set of variables.

THIS IS AN EXPERIMENTAL FEATURE.

Usage:

env = Environment()

with scaffold(createHeader("Page Title")) as html:
  with h1():
    # define a placeholder named 'title'
    text(env.define('title'))

env.title = 'Test Title'
# or
# env['title'] = 'Test Title'

print(html)

# finalizes on a certain thread.
env.dispose()
define(name)

Create a placeholder.

dispose()

Delete all data on the certain thread.

Placeholder class

class rattlepy.environment.placeholder.Placeholder(parent, name)

A class for placeholder in html node trees.

THIS IS AN EXPERIMENTAL FEATURE.