Say I have some string in a python file like:
myString = "Hello StackOverflow"
How I could access and use it in a separate html file like this generic template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
You really should use one of the many existing template libraries (Jinja being one of the most popular).
You can simply read in your template as a string and .format() it like any other, but this is error prone and you will run into many difficulties.
http://jinja.pocoo.org/docs/2.10/
https://www.makotemplates.org/
https://genshi.edgewall.org/
Related
My program generates some text and tables as HTML report files. Tables may vary in length depending on the week. I want the text under the tables to be on one page, it cannot be split into two pages when printed out.
I don't have any idea how to do this, thanks for any help.
This is a sample of my code:
<!DOCTYPE html>
<html lang="pl">
<head>
<title>Some title</title>
</head>
<style type="text/css">
Some style formating...
</style>
<body>
Some text......
{DF.to_html()}
Some text.....
{DF.to_html()}
Text that need to be on one page when printed
</body>
</html>
This question already has answers here:
How to convert plain text to html but keep the display format?
(2 answers)
Closed 1 year ago.
I've been trying to make newline on JSON Format, but none of it doesn't work.
from flask import *
import json
app = Flask(__name__)
#app.route("/user/", methods=["GET"])
def user():
datajson = {"Author": "Stawa",
"Version": "0.1.7"}
json_format = json.dumps(datajson, indent=6)
return render_template("index.html", json_format=json_format)
in index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST</title>
<meta charset="utf-8">
</head>
<body>
<p>{{ json_format }}</p>
</body>
</html>
The out put was {"Author": "Stawa", "Version": "0.1.7"}, but I want to make it as
{
"Author": "Stawa",
"Version": "0.1.7"
}
It's because the browser interprets your JSON string as HTML, and it doesn't care about newline characters. To preserve formatting, you can use the HTML <pre> tag. I tested the following template with your code, and it displays the JSON on two lines. Hope it works on your computer as well!
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST</title>
<meta charset="utf-8">
</head>
<body>
<pre>{{ json_format }}</pre>
</body>
</html>
Output:
This question already has answers here:
Parsing HTML using Python
(7 answers)
Closed 1 year ago.
How can one extract the things/ the content you see on a Webpage into a String
For Example turning this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>myWebpage</title>
</head>
<body>
<p>this</p>
<p>is</p>
<p>an</p>
<p>example</p>
</body>
</html>
Into a string that looks like this:
this is an example
This program does what you want : https://github.com/Alir3z4/html2text
You can also try something like:
import nltk
from urllib import urlopen
url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urlopen(url).read()
raw = nltk.clean_html(html)
print(raw)
This for example extracts the text from this webpage.
You can use selenium, find the documentations here: https://pypi.org/project/selenium/
I am transferring data from a python sheet to an HTML page using Flask. However, the corresponding code in the HTML file is not even changing color as it is supposed to, I don't think it's registering it as valid code. It is just treating the code as a string and outputting the code on the screen.
Here is the Python code in app.py:
from flask import Flask, render_template
name = ("Rachel", "Lola")
#app.route('/', methods = ['GET', 'POST'])
def data():
return render_template(Visualization.html, name=name)
Here is the HTML code that's in a file called Visualization.html:
{% for element in name %}
<p>{{element}}</p>
{% endfor %}
Any ideas on how to make this transfer the data properly? Thank you so much.
Your html file should look like this,if you are new to web dev, you should know that every html file should look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Your title</title>
</head>
<body>
{% for element in name %}
<p>{{element}}</p>
{% endfor %}
</body>
</html>
you have an error in the python file... you should type it as follow
return render_template('Visualization.html', name=name)
Sorry for my English.
I am want to study python library (eel). I am wrote the following code as in the example from the documentation:
The "index.html" file is located in the "Web" folder:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello</title>
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript">
eel.python_func("Hello from Python");
</script>
</head>
<body>
Hello!!
</body>
</html>
The "main.py" file is located one level above the "index.html" file:
import eel
eel.init("web")
#eel.expose
def python_func(text):
print(text)
eel.start("index.html")
Running main.py displays a browser window that says "Hello !!" but python_func() dont started. Message "Hello from Python" dont displayed in terminal.
I am tried different options for solutions to problem:
reinstalled eel
reinstalled python
pasted the python_func () function code right after import.
But this solutions not helped.
I ask for help in solving my problem.
I am using Python 3.7.6 32 bit