Formatting HTML style in PyCharm
<p>How to automatically generate tabs to get a proper tree view on a generated HTML file?</p>

<p>For example, I've got a code like this:</p>

<pre><code>    def creating_html(self):
    something ='''&lt;table style="width:100%"&gt;
    &lt;tr&gt;
        &lt;th&gt;Firstname&lt;/th&gt;
        &lt;th&gt;Lastname&lt;/th&gt;
        &lt;th&gt;Age&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Jill&lt;/td&gt;
        &lt;td&gt;Smith&lt;/td&gt;
        &lt;td&gt;50&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Eve&lt;/td&gt;
        &lt;td&gt;Jackson&lt;/td&gt;
        &lt;td&gt;94&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
'''
    file.write(something)
</code></pre>

<p>This code will generate code with proper tabs etc. but I feel my code is really messy because of that and also I think that I'm wasting time to check for a good format. So I was thinking about something like this, to make my code little bit cleaner.</p>

<pre><code>    def creating_html(self):
    something ='''
            &lt;table style="width:100%"&gt;
            &lt;tr&gt;
            &lt;th&gt;Firstname&lt;/th&gt;
            &lt;th&gt;Lastname&lt;/th&gt;
            &lt;th&gt;Age&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
            &lt;td&gt;Jill&lt;/td&gt;
            &lt;td&gt;Smith&lt;/td&gt;
            &lt;td&gt;50&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
            &lt;td&gt;Eve&lt;/td&gt;
            &lt;td&gt;Jackson&lt;/td&gt;
            &lt;td&gt;94&lt;/td&gt;
            &lt;/tr&gt;
            &lt;/table&gt;
            '''
    file.write(something)
</code></pre>

<p>but now the format is of course invalid.</p>

<p>I was searching for a good solution and one of them is just open this HTML file after generating it with PyCharm and use shortcut <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>L</kbd>. I'm looking for a solution to do it automatically.</p>

<pre><code>from lxml import etree, html
html_string ='''
            &lt;table style="width:100%"&gt;
            &lt;tr&gt;
            &lt;th&gt;Firstname&lt;/th&gt;
            &lt;th&gt;Lastname&lt;/th&gt;
            &lt;th&gt;Age&lt;/th&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
            &lt;td&gt;Jill&lt;/td&gt;
            &lt;td&gt;Smith&lt;/td&gt;
            &lt;td&gt;50&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
            &lt;td&gt;Eve&lt;/td&gt;
            &lt;td&gt;Jackson&lt;/td&gt;
            &lt;td&gt;94&lt;/td&gt;
            &lt;/tr&gt;
            &lt;/table&gt;
            '''
html_object = html.fromstring(html_string)
file.write(etree.tostring(html_object, pretty_print=True))
</code></pre>

python html python-2.7 pycharm