Using PYH to generate HTML documents

Source: Internet
Author: User
Tags html header

Recently in the project you need to export the results to HTML, found the library when searching the Internet, through some official documents and online blog to find its use is very simple, so choose to use it in the project.
In the use of the time found there are some problems in Python3, many places on the internet is not mentioned, so I am here to use it and I encountered problems and solutions for your reference
This article mainly refer to Pyh Chinese document
The downloaded sample is also the address mentioned in the article

General use

The module is generally imported first when it is used:

fromimport*

You can then create a Pyh object like this

= PyH(title)

Where title is a string, the string will be displayed as the title of the page, that is, the resulting HTML code is in the head with a title tag and the string as a literal value
Then we can addcss method or Addjs method into the external JS file or CSS file (call these two functions will produce an introduction code in the HTML header, for the kind of the body to add style code, I do not find any way)
Then is to create the label object, the corresponding tag class name is the same as in the HTML corresponding name, the parameter passed in the object is the property in the label, in addition to the class attribute corresponding to the parameter name is CL, the remaining parameter name corresponds to the attribute one by one in the HTML. Like we're going to create a div tag that can be written like this

= div(‘测试div‘id=‘div1‘="cls_div")

The resulting HTML code is as follows:

<div id=‘div1‘ class=‘cls_div‘>测试div</div>

Adding an element to an element allows you to use the << symbol, which returns the last contained symbol object. Like this.

div(id=‘div1‘<< p(‘测试‘=‘p_tag‘)

This code returns the object corresponding to the P element, and the resulting HTML code is as follows:

<div id=‘div1‘>    <p class=‘p_tag‘>测试</p></div>

When the appropriate HTML document is generated, you can use the PrintOut method to print it, or you can use the Render function to return the corresponding HTML code for us to save or to do further processing
Above is simply to do a brief introduction, the detailed use of the method please refer to an article mentioned above, which is written in more detail. Here's an example code that shows how I handled some of the errors that occurred, made some simple extensions, and looked at the source code in general.

Example
 fromPyhImport *ImportCodecs fromXml.sax.saxutilsImportEscapeword_width=  -defCreate_base (Table_title, page): Page.addcss (' Base.css ')#展示信息的表Base_table=Page<<Table (CL= ' diff ',ID = ' Difflib_chg_to0__top ', cellspacing= ' 0 ', cellpadding= ' 0 ', rules= ' groups ') forIinch Range(4): base_table<<Colgroup ()#表头T_head=Base_table<<Thead () Tr_tag=T_head<<TR () Tr_tag<<Th (CL= ' Diff_next ')<<BR () Tr_tag<<Th (table_title, colspan= ' 2 'Cl= ' Diff_header ') T_body=Base_table<<Tbody ()returnT_body#写入一行信息defWrite_line (Tr_tag, Mark, data): Tr_tag<<TD (Mark, CL= ' Diff_header ') Tr_tag<<TD (Data)defTxt2html (title, Table_title, IFile, ofile): I_f=Codecs.Open(IFile,' R ', encoding=' Utf-8 ') lines=I_f.read (). Splitlines () i_f.close () page=Pyh (title) T_body=Create_base (Table_title, page) Lineno= 1     forDatainchLinesif Len(data)>=Word_width: forIinch Range(Len(data)//Word_width+ 1): Sub_data=Data[word_width*Imin(Word_width*(I+ 1),Len(data)- 1)]ifI== 0: Mark= Str(Lineno)Else: Mark= ' > 'Tr_tag=T_body<<TR () Sub_data=Escape (Sub_data) sub_data=Sub_data.replace (" ","&nbsp;") Sub_data=Sub_data.replace ("\ t","&nbsp;&nbsp;&nbsp;&nbsp;") Write_line (Tr_tag, Mark, Sub_data)Else: Tr_tag=T_body<<TR () data=Escape (data) data=Data.replace (" ","&nbsp;") data=Data.replace ("\ t","&nbsp;&nbsp;&nbsp;&nbsp;") Write_line (Tr_tag,Str(Lineno), data) Lineno+= 1Html=Page.render () O_f=Codecs.Open(Ofile,' W ', encoding= ' Utf-8 ') O_f.write (HTML) o_f.close ()

This is an example of the conversion of any text file into an HTML document, mainly in the call txt2html function, the function has 4 parameters, the title of the page, the title of the table showing the text content, the input file path, the output file path
At the same time, some simple processing, marking each line in the original document, while setting a line to display only 100 characters extra line to wrap, in order to read
The resulting HTML opens up roughly as follows:

Running directly in the PYTHON3 environment found it reported an error:

There is a difference between a Unicode string and a normal string in Python2, but all strings in Python3 are Unicode by default, which cancels the Unicode function in the Python2, which is the main reason for the error, so we locate the error place, Modify the code to remove the Unicode function (in Python2, the Unicode function needs to pass in a normal string, so here we just need to remove the Unicode function, preserving the original parameters, for the word symbol conversion of the direct comment or change to pass can be
After resolving the Unicode issue, run again and report the error

Navigate to the corresponding code where there is a piece of code in the original code:

def TagFactory(name):    class f(Tag):        = name    f.__name__= name    return= modules[__name__]forin tags:    setattr(thisModule, t, TagFactory(t))

From this code, we can know that whenever we create a tag by the corresponding name, we find the corresponding tag inside tags, and then call the factory method to generate a corresponding tag, the factory method is actually a tag object, and all the HTML tags are the tag class, So you can guess if you want to add a new tag object, then you can modify tags inside the value, we add the corresponding tag value after the code can be run, so that the problem is resolved.
In fact, these errors are Python2 code porting to the PYTHON3 environment common errors, as far as its source I do not see too understand, mainly it generated the label of this piece, I do not know why modified tags can be run, Python class factory concept I still do not understand, It seems to take time to fill in the basics.

Using PYH to generate HTML documents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.