0

How to automatically generate tabs to get a proper tree view on a generated HTML file?

For example, I've got a code like this:

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

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.

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

but now the format is of course invalid.

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 Ctrl+Alt+L. I'm looking for a solution to do it automatically.

2
  • @jonrsharpe thanks for editing and sorry for my messy english. I'm still learning. :) Commented Aug 31, 2016 at 14:02
  • 1
    How does that make your code cleaner? It makes the inline HTML harder to read. And who is going to be reading it once generated anyway? Commented Aug 31, 2016 at 14:04

2 Answers 2

2

You may use Beautiful Soup 4 installing it with pip and use it like follows:

from bs4 import BeautifulSoup
something ='''
        <table style="width:100%">
        <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
        </tr>
        <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td>50</td>
        </tr>
        <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
        </tr>
        </table>
        '''
messed_code= BeautifulSoup(something, 'html.parser')
file.write(messed_code.prettify())

Personally, I think the string looks better in the first case as far as it's more readable

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

4 Comments

actually I need to work not on a something, because I've got a lot of "something's" which is sticked after. That's why I need to work on this writed file, but I'll read more about this beautifulSoup I'm 99% sure it will work for me, thanks :)
I guess you could read the file and pass its string in order to beautify it with beautiful soup
Yeah, I think its a good solution, I'll try after dinner, really thanks :-)
Glad to help you! If you feel this is a suitable answer, please accept it as an answer to close the post :)
1
from lxml import etree, html
html_string ='''
            <table style="width:100%">
            <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Age</th>
            </tr>
            <tr>
            <td>Jill</td>
            <td>Smith</td>
            <td>50</td>
            </tr>
            <tr>
            <td>Eve</td>
            <td>Jackson</td>
            <td>94</td>
            </tr>
            </table>
            '''
html_object = html.fromstring(html_string)
file.write(etree.tostring(html_object, pretty_print=True))

Comments

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.