Multiple <html><body> </html></body> in same file - python

I have a multiple html files in one file:
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
and the result is that I get a messed up html file. How to correct this without removing tags from the rest. I am using python to generate the html file. If I use the self.response.out.write(function(query)) I get a nice html page.
If I use it a second time self.response.out.write(function(query2)) then the page gets distorted.

Have one HTML file per file. Anything else is invalid and won’t be processed properly.
If you’re not sure if your HTML files are valid, the W3C’s validator will tell you.

Related

How do you input and output text with Pyscript?

I’m learning py-script where you can use <py-script></py-script> in an HTML5 file to write Python Code. As a python coder, I would like to try web development while still using python, so it would be helpful if we could output and input information using py-script.
For example, could someone explain how to get this function to work:
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<div>Type an sample input here</div>
<input id = “test_input”></input>
<-- How would you get this button to display the text you typed into the input into the div with the id, “test”--!>
<button id = “submit-button” onClick = “py-script-function”>
<div id = “test”></div>
<div
<py-script>
<py-script>
</body>
</html
I would appreciate it and I hope this will also help the other py-script users.
I checked source code on GitHub and found folder examples.
Using files todo.html and todo.py I created this index.html
(which I tested using local server python -m http.server)
Some elements I figured out because I have some experience with JavaScript and CSS - so it could be good to learn JavaScript and CSS to work with HTML elements.
index.html
<!DOCTYPE html>
<html>
<head>
<!--<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />-->
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<div>Type an sample input here</div>
<input type="text" id="test-input"/>
<button id="submit-button" type="submit" pys-onClick="my_function">OK</button>
<div id="test-output"></div>
<py-script>
from js import console
def my_function(*args, **kwargs):
#print('args:', args)
#print('kwargs:', kwargs)
console.log(f'args: {args}')
console.log(f'kwargs: {kwargs}')
text = Element('test-input').element.value
#print('text:', text)
console.log(f'text: {text}')
Element('test-output').element.innerText = text
</py-script>
</body>
</html>
Here screenshot with JavaScript console in DevTool in Firefox.
It needed longer time to load all modules
(from Create pyodine runtime to Collecting nodes...)
Next you can see outputs from console.log().
You may also use print() but it shows text with extra error writing to undefined ....
An alternative to way to display the output would be to replace the
Element('test-output').element.innerText = text
by
pyscript.write('test-output', text)

I would like to insert a html from result of pandas into an entire html

I hope someone will be able to help me form following issue that I am facing.
I just would like to insert a html from result of pandas into an entire html as below,
...
df_add_html=df_add.to_html()
template="""<html>
<head></head>
<body>
Pandas : left-only<br>
I want to insert df_add_html here
</body>
</html>"""
part1=MIMEText(template. 'html')
msg.attach(part1)
...
The purpose is that I want to merge many results from pandas to a html and I will send a email with the html.
I hope I would get some advice from you.
Thanks.
You can use standard format()
template = "text {} text".format(df_add_html)
or f-string
template = f"text {df_add_html} text"
template = """<html>
<head></head>
<body>
Pandas : left-only<br>
{}
</body>
</html>""".format(df_add_html)
or
template = f"""<html>
<head></head>
<body>
Pandas : left-only<br>
{df_add_html}
</body>
</html>"""
BTW: http://pyformat.info
If you need more complex templates (ie. with for-loop or if/else) then you can use jinja which is used to generate HTML in Flask (but you can generate any text file)

flask reading text file and view it in html page as lines [duplicate]

This question already has answers here:
Jinja render text in HTML preserving line breaks
(3 answers)
Closed 3 years ago.
my python code
from flask import Flask,render_template,request
import os,glob
f=open('filename.txt','r')
g= f.read()
app= Flask(__name__)
#app.route('/')
def index():
return render_template('index.html',n=g)
if __name__=="__main__":
app.run(debug=True)
and this is my html code
<html>
<head>
<title>admin</title>
</head>
<body>
<p>{{n}}</p>
</body>
</html>
it's shows me the output as one single line but I want it to show the lines Below each other.
This is more of an HTML problem.
You'll need to introduce HTML line breaks (<br>) in the rendering of the object.
Try replacing \n in your file w/ <br> (e.g., g = g.replace('\n', '<br>')).
(Or you can insert breaks programatically)
Also, in your HTML, change {{ n }} to {{ n|safe }}.
You may want to read more on Jinja formatting here: https://jinja.palletsprojects.com/en/2.10.x/templates/
What you are looking for is an HTML tag called pre
Try this :
<html>
<head>
<title>admin</title>
</head>
<body>
<div>
<pre>{{n}}</pre>
</div>
</body>
</html>
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
You should have more information in this question:
where are you viewing this output? in a terminal, in the browser after running flask's wsgi app?
are you trying to view the raw html text or formatted html like a web browser?
what version of Flask are you using?

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>

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.

Categories

Resources