Handling text and a new line character in Python's dominate module - python

I am using dominate module in Python 3.7, I am not sure how to handle the new line characters that are existing in Python. As per my requirement the new line character \n should be converted to break character in HTML <br>. But that is not happening. The dominate module is ignoring the newline character which is not how I am expecting it to behave. Below is the code which I have tried.
import dominate
from dominate.tags import *
text = "Hello\nworld!"
doc = dominate.document(title='Dominate your HTML')
with doc:
h1(text)
with open("dominate22.html", 'w') as file:
file.write(doc.render())
The output HTML code is
<!DOCTYPE html>
<html>
<head>
<title>Dominate your HTML</title>
</head>
<body>
<h1>Hello,
World!</h1>
</body>
</html>
Also I have tried replacing the new line character with break character i.e text.replace("\n", "<br>")
But this was creating a string like Hello<br>World, which was not what I was expecting. Attaching the HTML code for the same.
<!DOCTYPE html>
<html>
<head>
<title>Dominate your HTML</title>
</head>
<body>
<h1>Hello<br>world</h1>
</body>
</html>

The dominate module is ignoring the newline character which is not how I am expecting it to behave.
For the dominate library the \n is just a character in a text string. It's not the same as a line break <br> HTML element so you have to do add it programmatically.
This example shows two approaches, using a context manager and adding nodes to an instance:
import dominate
from dominate.tags import h1, h2, br
from dominate.util import text
the_string = "Hello\nworld!"
doc = dominate.document(title='Dominate your HTML')
with doc:
parts = the_string.split('\n')
with h1(): # using a context manager
text(parts[0])
br()
text(parts[1])
header = h2() # same as above but adding nodes
header.add(text(parts[0]))
header.add(br())
header.add(text(parts[1]))
with open("dominate22.html", 'w') as file:
file.write(doc.render())
Gives this HTML:
<!DOCTYPE html>
<html>
<head>
<title>Dominate your HTML</title>
</head>
<body>
<h1>Hello<br>world!</h1>
<h2>Hello<br>world!</h2>
</body>
</html>

Related

How make it display the entire contents of the normal when use xhtml2pdf?

When the contents of the <p> paragraphs in the html code are too long,
How can I make it display the entire contents of the normal?
No artificial add tags,eg: <br>. I tried to use css to limit the maximum
length, and automatic word wrap, but it still can not.
test case:
# -*- coding: utf-8 -*-
import xhtml2pdf.pisa as pisa
pisa.showLogging()
def dumpErrors(pdf, showLog=True):
if pdf.warn:
print "*** %d WARNINGS OCCURED" % pdf.warn
if pdf.err:
print "*** %d ERRORS OCCURED" % pdf.err
def testlong(src="example.html", dest="example.pdf"):
pdf = pisa.CreatePDF(file(src, "r"), file(dest, "wb"))
dumpErrors(pdf)
if not pdf.err:
pisa.startViewer(dest)
if __name__=="__main__":
testlong()
the example html code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
<style>
body{
font-family: 'Gilles';
}
.testdiv {
width:100%;
max-width:500px;
word-break:break-all;
}
</style>
</head>
<body>
<div class="testdiv">
<p>
"sdfsdfsdfffffffffffffffffffffffffffwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwggggggggggggggggggggggggpppppppppppaaaaaaazzzzzzzzzzxxxxxxxccccccvvvvvvvbbbbnnnnnnnnmmmmmkkkkkkkkllllll"
</p>
</div>
</body>
</html>
the PDF:
enter image description here
Try this pdfkit
Install this also wkhtmltopdf
import pdfkit
pdfkit.from_file('example.html', 'example.pdf')

NPP + Python: Move Text between search strings to Another Position (Footer)

I am working on sone html-documents looking like this:
<html>
<head>Something in here</head>
<body>
<MYTAG>This should be moved to the Footer</MYTAG>
<MYTAG>This should be moved to the Footer, too</MYTAG>
</body>
<footer></footer>
</html>
I am already using Notepad++ and Python to customize the rest of the document mainly using Regular Expressions.
Now I want to move the parts that are tagged with <MYTAG></MYTAG> to the footer, having the documents like this in the end:
<html>
<head>Something in here</head>
<body>
</body>
<footer>
<MYTAG>This should be moved to the Footer</MYTAG>
<MYTAG>This should be moved to the Footer, too</MYTAG>
</footer>
</html>
First I tried to do the job with Regular Expressions alone:
Search for:
(<html.*?)(<MYTAG>.*?</MYTAG>)(.*?<footer>)(.*?)(</footer>.*?</html>)
and replace it with: $1$3$4$2$5
This works, but I have to run it over and over again for multiple <MYTAG>-parts (and it's a pain... with larger documents).
I know there is a better solution with python but I cannot get the coding write. The documentation and Syntax confuses me. I thought about using editor.setSelection followed by editor.cut and finally editor.paste somewhere to the footer but I don't know how to set the right targets.
Any help on this is very much appreciated :)
You can use following script:
import re
with open("temp.html") as html_file:
html = html_file.read()
tags = re.findall(r"<MYTAG>.*</MYTAG>\n*", html)
html = re.sub(r"<MYTAG>.*</MYTAG>\n*", "", html)
footer = re.split(r"<footer>", html)
tags.insert(0, "<footer>\n")
tags.insert(0, footer[0])
tags.append(footer[1])
with open("temp.html", "w") as html_file:
html_file.write("".join(tags))
It working following way:
Read file
Finds all tags
Replaces tags in the body
Split file's content on 2 parts.
Adds the tags and <footer> in the text
Writes result to the file.
Try this
(<html.*?)((?:\s*<MYTAG>[^<]+<\/MYTAG>\n*)+)(.*?( *)<footer>)(.*?)(<\/footer>.*?<\/html>)
Substitution:
\1\3\2\4\6
Regex Demo
Input
<html>
<head>Something in here</head>
<body>
<MYTAG>This should be moved to the Footer</MYTAG>
<MYTAG>This should be moved to the Footer, too</MYTAG>
</body>
<footer></footer>
</html>
Output
<html>
<head>Something in here</head>
<body> </body>
<footer>
<MYTAG>This should be moved to the Footer</MYTAG>
<MYTAG>This should be moved to the Footer, too</MYTAG>
</footer>
</html>

Issue with HTTP GET Parameters in Python CGI

I am learning web development in Python. When I open the HTML I get the Ferrari Fiat Ford, which is what I am expecting but then I click on Ferrari and it opens up the new page as make, model, which is not what I want. I want Ferrari Dino.
Could you help me understand what is the problem?
<!DOCTYPE HTML>
<html lang ="en">
<head>
<meta charset="UTF-8">
<title>Python Response</title>
</head>
<body>
<h1>
Ferrari
<a href = 'get.py?make = Fiat & model = Topolino'>Fiat</a>
<a href = 'get.py?make = Ford & model = Mustang'>Ford</a>
</h1>
</body>
</html>
Python
import cgi
data = cgi.FieldStorage()
make = data.getvalue('make')
model = data.getvalue('model')
print ( 'Content-type:text/html\r\n\r\n' )
print ( '''<!DOCTYPE HTML><html lang = "en">
<head>
<meta charset="UTF-8">
<title>Python Response</title>
</head>
<body>
<h1>, make, model,</h1>
Back
</body>
</html>''' )
A variable cannot be a string.
import cgi
data = cgi.FieldStorage()
make = data.getvalue('make')
model = data.getvalue('model')
print ( 'Content-type:text/html\r\n\r\n' )
print ( '''<!DOCTYPE HTML><html lang = "en">
<head>
<meta charset="UTF-8">
<title>Python Response</title>
</head>
<body>
<h1>, ''' + make + ', ' + model + ''',</h1>
Back
</body>
</html>''' )
After testing this out, the HTML is responding as anticipated.
My recommendation goes along lines of this question here: How to pass python variable to html variable?
In summary, you could do this in a better way by writing a function and returning the html. Also, once you've written the python function, rather than have the:
<h1>, make, model,</h1>
You could use a substitution in your python function like so:
"<h1>, %s, %s,</h1>" % (make, model)
Which would be located in the same python file as the two of these variables.
Your template outputs this:
<h1>, make, model,</h1>
And that's all it ever will output. You can instead change it a little and then use the format method to insert values into it.
html = '''<!DOCTYPE HTML><html lang = "en">
<head>
<meta charset="UTF-8">
<title>Python Response</title>
</head>
<body>
<h1>, {make}, {model},</h1>
Back
</body>
</html>'''
print html.format(make=make, model=model)
The values in curlybraces, {make} and {model}, are named tokens inside the string. When you use format() on the string called html, you replace those tokens. In this case, I referenced them directly by name.

Is there a way of writing several lines at a time to a file in python?

I need to write a lot of information to a file, basically a whole webpage with certain values calculated using my script. I know I can do this using .write(), however I would like to know if you can write several lines at a time to a file, without having to put in all of the line breaks.
For example, I would like to wite the following to a file:
<!DOCTYPE html>
<html>
<head>
</head>
<style>
some styling stuff ..
<\style>
<body>
many more lines of code ...
</body>
</html>
Currently I have
file = open('filetowriteto.txt','w')
file.write('<html>\n')
file.write('<head>\n')
...
file.close()
But I would like to be able to do
file.write('
<html>
<head>
</head>
<style>
some styling stuff ..
<\style>
<body>
many more lines of code ...
</body>
</html>')
Does anybody know of a way to do this? Thanks!
When you use triple quotes ('''), line breaks are read into the string:
file.write('''
<html>
<head>
</head>
<style>
some styling stuff ..
<\style>
<body>
many more lines of code ...
</body>
</html>''')
That's what file.writelines is for:
with open(filename) as fp:
fp.writelines([
'<html>',
'</html>'
])
You also could use multiline strings with triple quotes ''' or """, but they tend to mess with indentation.
That being said, consider using Jinja for HTML output.

Append before closing body tag in python

ok guys so I have a template.html file like so:
<h1>Hello wolrd</h1>
<div>This is me</div>
And I want to append that to my index file before the closing body tag. Just like so:
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script type="text/ng-template" id="templates/template.html">
<h1>Hello wolrd</h1>
<div>This is me</div>
</script>
</body>
</html>
I've so far gotten to read the file and append to the end of it but I have yet to add the script tags to the file that I am reading and append to the correct spot of my file. This is what I currently have:
#!/usr/bin/env python
import fileinput
to_readfile=open('index.html', "r")
try:
reading_file=to_readfile.read()
writefile=open('index2.html','a')
try:
writefile.write("\n")
writefile.write(reading_file)
finally:
writefile.close()
finally:
to_readfile.close()
Any help would be much appreciated. Thank you!
The simplest approach would be to add a placeholder in the layout template and then when processing the layout search for the placeholder and replace it with the contents of the other template.
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<script type="text/ng-template" id="templates/template.html">
{{content}}
</script>
</body>
</html>
...
..
.
layout = open('layout.html', "r")
layout_contents = layout.read()
partial=open('partial_file.html','r')
result = layout_contents.replace("{{content}}", partial)
writefile = open("file_to_write.html", "w")
writefile.write("\n")
writefile.write(result)
.
..
....
You can also work on a much more extensive solution such as the ones used by jinja http://jinja.pocoo.org/docs/templates/#template-inheritance.

Categories

Resources