Unicode characters from SQLite to HTML - python

I'm building a web page using CGI and Python (yes, I know, horrible combination!). I have some unicode data stored inside my SQLite 3 database, which I load in my Python script. When it's time to combine those unicode characters with HTML, I'm viewing things like this in my web browser:
\xc3\xb3
Instead of:
ó
I think the problem isn't in the HTML code, because I have defined the encoding like this:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
This is how I'm rendering the HTML code (using web.py's templating system):
render = web.template.render("templates")
return render.index(name)
Where name is:
name = cursor.execute("SELECT name FROM names").fetchone()
And the template (index.html):
$def with (name)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
Hello, $name!
</body>
</html>
How can I achieve this?

Related

CSS Not Loading in Python

I'm creating a simple page using python 3.4 and generating HTML. However the stylesheets don't load. Here is my code:
`#!/Python34/python
content ="""<html>
<head>
<meta charset="utf-8">
<title>Spain National Team</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<!---Body Stuff--->
</body>
</html>"""
print(content)`
Checking the source of the webpage,it's a well formed HTML document, but the styles don't load. My python script and css are in the same directory:
xampp/cgi-bin/sports/index.py
and
xampp/cgi-bin/sports/styles.css
If you want to open it on your default browser,
import webbrowser
content ='<html>\n<head>\n<meta charset="utf-8">\n<title>Spain National Team</title>\n<link rel="stylesheet" href="styles.css"/>\n</head>\n<body>\n<button></button>\n</body>\n</html>'
html_file = open('sample.html', 'w')
html_file.write(content)
flnme="sample.html"
webbrowser.open_new_tab(flnme)

django calling a python script shows the html code instead of a webpage

my django app calls a python script(query.cgi). but when I run it, the website shows the html printout from that script instead of showing the output as a webpage.
def query(request):
if request.method == 'GET':
output = subprocess.check_output(['python', 'query.cgi']).decode('utf-8')
return HttpResponse(output, content_type="text/plain")
The webpage shows:
Content-Type: text/html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="css/css_4.css" media="screen" />
<title>....</title>
</head><body>.....</body></html>
Thanks for any help!!
return HttpResponse(output, content_type="text/plain")
The reason it's returning escaped HTML is because you have content_type='text/plain', which says you are just sending plain text.
Try changing it to 'text/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.

Alternative to u'' for unicode strings

I have the following asp script which uses python 2.5:
<%# Language = Python CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="sv" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
Jellö Wörld<br>
<%
Response.Write(u'Hellö Wörld<br>')
%>
</body>
</html>
It works correctly, hurrah! However, it will become annoying if I have to use u'' all over the place. What alternatives are there? Is there any future I can import so that I can have python3 like strings?
Thanks for your help,
Barry.
from __future__ import unicode_literals
However, you will need 2.6 or later for this to work.

Unicode strings in tornado web app

How can I use unicode strings in tornado views or templates?
I insert in template
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
And in view# -- coding: utf-8 --
Output is ????
Once you have your unicode string ready, the request should end
self.render("template.html", aString=aUnicodeString)
This renders the file "template.html" setting the aString variable to aUnicodeString.
template.html would look something like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
{{aString}}
</body>
</html>
It's also possible to inline the HTML in the Tornado server.
self.render('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>{{aString}}</body></html>', aString=aUnicodeString)
More on templates here:
Tornado Web Server Documentation

Categories

Resources