skip to Main Content

I have the following code that is supposed to indent the data like this:

Searching for: 5456
    Child Unit ID: 32455
    Unit Location: Test

I have written the code to achieve this indentation but for some reason the indentation is not working when I am viewing it on web page. This python code is being used in Flask

Code:

temp_storage = {}
list_output = []

def printingData(Unit, increaseSpaceLength, source="."):
    theSpace = '       '
    spaceLength = theSpace*increaseSpaceLength
    list_output.append(f"{spaceLength}Searching for {Unit}:n")
    for path in glob.glob(os.path.join(source,"x*.xml")):
        theDocument = temp_storage.setdefault(path, ET.parse(path).getroot())       
        if(theDocument.get("ID")== Unit):
            theSpot = theDocument.findall('.//xTest[@Type="Unit"]')
            if len(theSpot) == 0:
                list_output.append(f'{spaceLength}No sub Units found for {Unit}n')
            else:
                increaseSpaceLength +=1
                spaceLength = theSpace*increaseSpaceLength
                for unit_element in theSpot:
                    unit_id = unit_element.get("Unit")
                    location = unit_element.get("Name")
                    if unit_id != "":
                        list_output.append(f'{spaceLength}Child Unit ID:{unit_id}n')
                        list_output.append(f'{spaceLength}Unit Location:{location}n')
                        printingData(unit_id, increaseSpaceLength= increaseSpaceLength+1,source=source)
    return list_output 

Flask:

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        name = request.form.get('name')
        
        return_string = printingData(name, source=r"C:/.../.../")
        return render_template('my-form.html', result=return_string)
    return render_template('my-form.html', result='')

if __name__ == '__main__':
    app.run()

HTML:

<body>   
   <ol>  
       <!-- For loop logic of jinja2 template -->   
       {%for i in result%}   
       <li>{{i}}</li>   
       {%endfor%}   
   </ol>   
</body>

This is the current output:

Searching for: 5456
Child Unit ID: 32455
Unit Location: Test

I am not exactly sure what I am missing in order for the indentation to work.

2

Answers


  1. Your problem comes down to html not displaying leading spaces as you expect. Try replacing theSpace = ' ' with:

    theSpace = '&nbsp;' * 7
    
    Login or Signup to reply.
  2. In HTML, whitespace is collapsed by default, so multiple spaces will appear as a single space. You can use a special space, called &nbsp;, which HTML will respect.

    Here’s how to fix:

    Python

    In your Python code, replace:

    theSpace = '       '
    

    with:

    theSpace = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    

    HTML

    In your HTML, change:

    <li>{{i}}</li>
    

    to:

    <li>{{ i|safe }}</li>
    

    This should make the spaces appear on your webpage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search