Skip to main content

Sierra is a Python library to write HTML and CSS in pure Python using the DOM API in a simple yet elegant manner

Project description

sierra

Sierra

GitHub

Sierra is a Python library to write HTML and CSS in pure Python using the DOM API in a simple yet elegant manner. Take advantage of Python's powerful functionalities with ease. Loops, variables, functions, libraries - you name it, you have it.

Here are a few advantages of using Sierra over other Python libraries that use the DOM API:

  • Out-of-the-box support for all CSS styling attributes for all tags
  • Display a table by simply putting in a CSV file
  • Create your own tag functions with absolute ease using @tag and @CmTag. You can decide their behavior and use them within content-managers too
  • Improvement in the arrangement look of the code and intelligent handling of tags with
    autoPrettify() - Powered by bs4 and a feature like no other

You may also use this as a templating engine, although jinja and Django's templating engine is strongly recommended over this


Documentation


Installation and Upgrade

pip install sierra

pip install --upgrade sierra

Starting off is pretty simple and straightforward:

from sierra import *
    
title('Hello World!')

The title() function at the start is mandatory, since it commences the HTML and the CSS file, which is created in the working directory upon execution on the code.

You can create custom tag functions with @tag and @CmTag with just three lines of code. Say you want to create a function for <meta>:

@tag
def meta(**kwargs):
    pass
        
# Using them
    
meta(name="description", content="This is some description")
meta(name="viewport", content="width=device-width", initial_scale=1.0)

Underscores are used for hyphens (same applies to CSS) and Python-conficting arguments are prefixed with a double underscore.

Using argument text inside of a function defined in @tag will create a tag that opens, enters text, and closes. Something like this:

@tag
def script(**kwargs):
    pass
script(__async="", src="some_src", text="some_text")

Is the equivalent of:

<script async="" src="some_src">some_text</script>

Want to add some JS? Simple enough. Just create a function for the <script> tag with a context manager behavior using @CmTag and you're golden.

@CmTag
def script(**kwargs):
    pass

# Here I'll be replicating the script needed to add Google Analytics to a webpage

with script(__aync="", src="https://www.googletagmanager.com/gtag/js?id=UA—XXXXXXXX-X"):
    pass
    
with script():

    writeWA('''
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA—XXXXXXXX-X');
  ''')

This is the equivalent of:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA—XXXXXXXX-X"></script>

<script>
    
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA—XXXXXXXX-X');
    
</script>

writeWA() writes text entered into it into the HTML file as it is.

You can add fonts using addFont()

addFont("https://fonts.googleapis.com/css2?family=Roboto&display=swap")

Once things at the <head> of the HTML are settled (CSS is automatically linked), begin the body of the HTML with

openBody()
# You can add any number of styling arguments to the body within openBody()
openBody(background_color='yellowgreen', opacity='0.9')

You can create div and section tags this way:

with div(__class="some_class") as d:
    p('This is a paragraph!')
    d.css(background_color="#5886d1")

Let's break this down but-by-bit:
First, we start a div with a context manager behavior and give it an attribute __class, which is essentially the tag attribute class (remember Python-conflicting) arguments are prefixed by a double underscore.

p() is a function, as the name suggests, to add a <p> tag. You can give the tag attributes with **kwargs, if you like.
p('Hello World!', __class='p_class') is the same as <p class="p_class">Hello World!</p>

After the paragraph, there's a d.css(). This adds CSS to the class mentioned within div(). If a class is mentioned, CSS is added to that class as the first priority. If an id is mentioned, CSS is added to that id as a second priority. If none of both are mentioned, CSS is just added to div.

The behavior of div shown above also applies to section.

You can open a new tag with Tag()

with Tag('some_tag', id='some_id') as t:
    p('A paragraph in <some_tag>')
    t.css(color='blue')

Although here, .css() behaves differently. It is independent of tag attributes, meaning CSS is added directly to the tag mentioned, which is some_tag

To add CSS to a specific attribute in the tag, use writeCSS()

writeCSS(tag_name, **kwargs)

writeCSS("#some_id", color='blue')

This adds CSS to the some_id.

You can add a table to the HTML page by inputting in a CSV file this way:

with Table() as t:
    t.get_table("path/to/file.csv")   # Add attributes with **kwargs here
    t.css(border="1px solid black")   # Use writeCSS to add CSS to a specific attribute

There are MANY more functionalities to Sierra that you can see from the documentation

At the end of all development with Sierra, use

autoPrettify()

It takes in no arguments, but provides SO much to the code in terms of handling tags and beautifying the look of it.


Contact Us

Email: brainstormyourwayin@gmail.com

Or you can contact either of us individually if you like. See our individual GitHub profiles for information.


GitHub


License

Copyright 2021 BrainStormYourWayIn

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Pandas (pandas)

Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team.
Copyright (c) 2011-2020, Open source contributors.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sierra-2.4.0.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

sierra-2.4.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file sierra-2.4.0.tar.gz.

File metadata

  • Download URL: sierra-2.4.0.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for sierra-2.4.0.tar.gz
Algorithm Hash digest
SHA256 93aa9a59c5f9cc0b3ff32d8d2f40901b27d8001e073d0e63f26e5c2f1acde2e9
MD5 daf6007d70fc82d7e487191c9c2ae924
BLAKE2b-256 2d30592c1c090a7b1ffe243aa447bbb3e0ac8414717de814a1efa5413d228974

See more details on using hashes here.

File details

Details for the file sierra-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: sierra-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for sierra-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be0b2b7ee580d0a3904d13153d5d9655de058ce735c4fcefc93b53c33bc3bf48
MD5 bd94768897263550a937c98c2639da9e
BLAKE2b-256 1c6ff036d4811f9c29b4902ab778a3fd404c806fb2a14dcfa3066b10fe3de76a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page