Samir
Samir

Reputation: 3187

I'm trying to create a website with python, in an odd way

Here's my code:

with open('index.html', 'w') as html:
    title = input('Title of the page? ').capitalize()
    h2 = input('Heading for your website: ').capitalize()
    p1 = input('First paragraph: ').capitalize()
    p2 = input('Second paragraph: ').capitalize()
    a = '''
<html>
    <head>
        <link href="css/bootstrap.css" rel="stylesheet">

        <title> {} </title>
    </head>
    <body>
        <center>
        <h2> {} </h2>
        </center>
        <div id='well'>
            <p style='position: relative; left: 100px'> {} </p>
            <p style='position: relative; left: 100px'> {} </p>
            <input type='button' value='test' onClick='Button()'>
            <p id='lol'> </p>
        </div>
        <script>
        function Button()
            \{
            document.getElementById('lol').innerHTML = 'hello';

            \}

        </script>
    </body>
</html>
'''.format(title, h2, p1, p2)
    html.write(a)

Everything worked perfectly fine until I attempted to add the javascript function with the { } brackets, and the program thought they were part of the format function, how do I get past this? I've tried putting \ infront of it, but it didn't work, (btw, any ideas I should add to this very simple concept? :) )

Upvotes: 1

Views: 98

Answers (2)

Map X
Map X

Reputation: 444

A workaround (and a good practice) is NOT mix JavaScript code into HTML. Instead, you leave hooks in HTML for JavaScript.

See http://developer.yahoo.com/performance/rules.html

Upvotes: 1

Rapolas K.
Rapolas K.

Reputation: 1709

You need to double curly braces. Instead of escaping with \, you should have {{ and }}.

Upvotes: 6

Related Questions