1

I am trying to create a table within a Google App Engine Application where the background color in a table changes periodically based on input. Does anyone know how to accomplish this? Here is my code:

    self.response.out.write("""
         <img src="/images/resistor.png" width = "150">
         <table border = "1">
         <tr height="150" >
         <td bgcolor="%s" width="35">  </td> <td bgcolor="%s" width="35">  </td> <td bgcolor="%s" width="35">  </td> <td bgcolor="%s" width="35"> </td> %(Red,Blue,Black,Green)
         </tr>
         </table>
          <form action="/sign" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form> """)
    self.response.out.write('</pre></body></html>')

For example the Red,Green... Colors in %( ) will be variables that will change so at one point they all may be Red or Blue and Yellow.

1 Answer 1

2

That type of string-formatting is deprecated. Please use the .format() method in new code. Example:

self.response.out.write("""
     <img src="/images/resistor.png" width = "150">
     <table border = "1">
       <tr height="150" >
         <td bgcolor="{}" width="35">  </td> 
         <td bgcolor="{}" width="35">  </td> 
         <td bgcolor="{}" width="35">  </td> 
         <td bgcolor="{}" width="35">  </td>
       </tr>
     </table>
     <form action="/sign" method="post">
       <div><textarea name="content" rows="3" cols="60"></textarea></div>
       <div><input type="submit" value="Sign Guestbook"></div>
     </form> """.format( ('Red','Blue','Black','Green') ))
self.response.out.write('</pre></body></html>')

And for anything beyond the basic have a look at using templates. Examples of templating systems are Jinja2 and Django Templates.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help, but I am getting the error File "/Users/ben/Desktop/theresistorpage/main.py", line 116, in post </form> """.format( ('Red','Blue','Black','Green') )) IndexError: tuple index out of range

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.