HTML question for beginners: colored background for symbols in Django HttpResponse
I would like to create an HttpResponse containing a specific string. For each of the characters in the string, I have a background color that I want to use.
For simplicity, let's say that I only have shades of green in the background and that the "background color" data represents the "brightness level" in the green domain.
For example the answer might be "abcd" and my "background color" data might be:
[0.0, 1.0, 0.5, 1.0]
This means that the first character "a" must have a background of dark green (eg 004000), the second character "b" must have a background of bright green (eg 00ff00), the third character "c" must have a "middle" brightness (e.g. 00A000), etc.
I don't want to use a template, but just return a plain text response. Is it possible?
If not, what would be the simplest template I could use for this?
thanks
a source to share
you can use something like this to generate html in the django view itself and return it as text / html
data = "abcd"
greenShades = [0.0, 1.0, 0.5, 1.0]
out = "<html>"
for d, clrG in zip(data,greenShades):
out +=""" <div style="background-color:RGB(0,%s,0);color:white;">%s</div> """%(int(clrG*255), d)
out += "</html>"
a source to share
Your best bet here is to use an element span
as well as a stylesheet. If you don't want to use a template, you will have to display this inline. Example:
string_data = 'asdf'
color_data = [0.0, 1.0, 0.5, 1.0]
response = []
for char, color in zip(string_data, color_data):
response.append('<span style="background-color:rgb(0,%s,0);">%s</span>' % (color, char)
response = HttpResponse(''.join(response))
I would suggest that this can also be done in a template if you wish.
a source to share