0

I'm currently developing an application which serves all kind of sensor data to the user via a web page. Python-CGI is used to create the HTML and JavaScript code dynamically.

Before creating it the python script uses str.format() to insert some data into the html.

For the sake of simplicity I've omitted some code.

page_source = """   
<!DOCTYPE html>
    <html>

    ...

            function print_array(code)
            {
                switch (code)
                {
                   case '001':
                       SetValueVisibilty('Buttonspan', {0})
                       break
                   case '002':
                          SetValueVisibilty('Ultraspan', {1})
                       break
                   case '003':  
                          SetValueVisibilty('Gasspan', {2})
                       break;
                   case '004':  
                          SetValueVisibilty('Vib1span', {3}) 
                       break
                   case '005': 
                          SetValueVisibilty('Vib2span', {4}) 
                       break
                   case '006':   
                       SetValueVisibilty('Soundspan', {5})      
                       break
                   case '007': 
                       SetValueVisibilty('Lightspan', {6})   
                       break
                   default: 
                       alert('No Sensor');
                       break
                }                
            }

            </script>                            
            <center>
            <ul id='Sensors'>   
            <li id='Button'>          
            <INPUT type='button' value='Button' onMouseOver='return print_array('001')' onMouseOut='clearspan('Buttonspan')'/>
            <span class='invisiblespans' id='Buttonspan'></span>              
            </li>      

        ...

    </body>
    </html>""".format(get_five_entries("001"),get_five_entries("002"),get_five_entries("003"),get_five_entries("004"),get_five_entries("005"),get_five_entries("006"),get_five_entries("007"))

 page_source = """  
<!DOCTYPE html>
    <html>

    ...

            function print_array(code)
            {
                switch (code)
                {
                   case '001':
                       SetValueVisibilty('Buttonspan', {0})
                       break
                   case '002':
                          SetValueVisibilty('Ultraspan', {1})
                       break
                   case '003':  
                          SetValueVisibilty('Gasspan', {2})
                       break;
                   case '004':  
                          SetValueVisibilty('Vib1span', {3}) 
                       break
                   case '005': 
                          SetValueVisibilty('Vib2span', {4}) 
                       break
                   case '006':   
                       SetValueVisibilty('Soundspan', {5})      
                       break
                   case '007': 
                       SetValueVisibilty('Lightspan', {6})   
                       break
                   default: 
                       alert('No Sensor');
                       break
                }                
            }

            </script>                            
            <center>
            <ul id='Sensors'>   
            <li id='Button'>          
            <INPUT type='button' value='Button' onMouseOver='return print_array('001')' onMouseOut='clearspan('Buttonspan')'/>
            <span class='invisiblespans' id='Buttonspan'></span>              
            </li>      

        ...

    </body>
    </html>""".format(get_five_entries("001"),get_five_entries("002"),get_five_entries("003"),get_five_entries("004"),get_five_entries("005"),get_five_entries("006"),get_five_entries("007"))

The function get_five_entries() returns a string.

I tried this both on a apache server and on the cli, but I'm always getting a KeyError:

KeyError: '\n margin'

I thought this could be caused by some misuse of quotes, so I've changed them all to single quotes, but still no success.

Could somebody please give me a hint to solve this?! UPDATE:

def get_five_entries(column_code):

statusCodes = {'001':'Button', '002':'Ultra Sonic Sensor', '003':'Gas Sensor',                                
                '004':'Vibration Sensor 1','005':'Vibration Sensor 2', '006':'Sound Sensor', '007':'Light Sensor'}    

if column_code in statusCodes:   
...
   curs.execute("SELECT * FROM Log WHERE Code=:Code",{"Code":column_code}) 
3
  • please show code, that produces the error. your example doesn't contain margin nor any key access.
    – Daniel
    May 1, 2014 at 18:52
  • there is no margin anywhere in the code... May 1, 2014 at 19:11
  • '\n margin' is most likely coming from the CSS code which was removed for simplicity.
    – kitti
    May 1, 2014 at 21:07

2 Answers 2

3

Like the comments said, there is no margin anywhere in that code, so that error is coming from somewhere else. But I'm guessing it's all related to the same error: { and } are delimiters for format. So when you have a JS or CSS block surrounded by { and }, format sees that as a placeholder - which isn't what you want.

So, anywhere you actually want real braces, double them up to escape them from format parsing. i.e.:

function print_array(code)
{{
    switch (code)
    {{
        ...
    }}
}}
0
2

i was getting same error in my code where i used html templates for sending emails using python smtp. I tried two things.

  1. use double {{}} for every variable you pass in template.
 template = """ <html>
<head> </head>
<body>
<p> Name of Criminal is {{x}} and the crime is {{y}}
</body>
 </html> """.format(x="Laden",y="Bombing in USA")
  1. use string concat after the tag ends.
 template = """ <html>
<head> </head> """ + """
<body>
<p> Name of Criminal is {{x}} and the crime is {{y}}
</body>
 </html> """.format(x="Laden",y="Bombing in USA")

Second one worked for me. In example i took random templates to explain which came in my mind , i don't work with FBI!!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.