The purpose of this python code is for the user to type in a unique ID to search for. The code will then iterate through a list of XML files contained in a folder called XMLContentFiles, and then it will display the result back in the terminal.
Sample Input: x632
Sample output:
Comp Unique ID: x123 Label of Page: Apple Name of Comp: Fruit
Comp Unique ID: x456 Label of Page: Water Name of Comp: Environment
Comp Unique ID: x789 Label of Page: Shirt Name of Comp: Clothing
Python Code:
import xml.etree.ElementTree as ET
import datetime
import os
import csv
import re
import glob
userInput = input("Enter ID to search:")
# calling myFunction
myFunction(userInput, source=r"C:/Sam/XMLContentFiles/")
temp_storage = {}
def myFunction(COMP, source="."):
for pathway in glob.glob(os.pathway.join(source,"x*.xml")):
document = temp_storage.setdefault(pathway, ET.parse(pathway).getroot())
theLocationPoint = document.findall('.//*[@Comp="' + COMP + '"]')
if theLocationPoint:
comp_unique_id = document.get("ID")
comp_label = document.tag
comp_name = document.get("Name")
print("Comp Unique ID: " + comp_unique_id + " " + "Label of Page: " + comp_label + " " + "Name of Comp: " + comp_name)
myFunction(comp_unique_id, source=source)
I have turned this script into a web application using flask:
HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="name">
<button type="submit">Submit</button>
</form>
<div>
<p>{{result}}</p>
</div>
</body>
</html>
Python Code:
from flask import Flask, request, render_template
import xml.etree.ElementTree as ET
import datetime
import os
import csv
import re
import glob
app = Flask(__name__)
temp_storage = {}
def myFunction(COMP, source="."):
for pathway in glob.glob(os.pathway.join(source,"x*.xml")):
document = temp_storage.setdefault(pathway, ET.parse(pathway).getroot())
theLocationPoint = document.findall('.//*[@Comp="' + COMP + '"]')
if theLocationPoint:
comp_unique_id = document.get("ID")
comp_label = document.tag
comp_name = document.get("Name")
myFunction(comp_unique_id, source=source)
return "Comp Unique ID: " + comp_unique_id + " " + "Label of Page: " + comp_label + " " + "Name of Comp: " + comp_name
# Creating a route that has both GET and POST request methods
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
name = request.form.get('name')
#calling myFunction
return_string = myFunction(name, source=r"C:/Sam/XMLContentFiles/")
return render_template('my-form.html', result=return_string)
return render_template('my-form.html', result='')
if __name__ == '__main__':
app.run()
Question: The problem I am running into is that this code prints only the first iteration of the loop to the web page.
For example if I input: x632
The Output is: Comp Unique ID: x123 Label of Page: Apple Name of Comp: Fruit
But the output should actually be:
Comp Unique ID: x123 Label of Page: Apple Name of Comp: Fruit
Comp Unique ID: x456 Label of Page: Water Name of Comp: Environment
Comp Unique ID: x789 Label of Page: Shirt Name of Comp: Clothing
Why is the code stopping after printing the first iteration? Is it ignoring the recursive call? I am thinking there is something wrong with these lines of code but I am not sure why:
myFunction(comp_unique_id, source=source)
return "Comp Unique ID: " + comp_unique_id + " " + "Label of Page: " + comp_label + " " + "Name of Comp: " + comp_name
2
Answers
You are returning the value before finishing the entire iteration. That is why it is printing only first iteration value.
I would suggest you to collect your iteration output in a list and return the list once iteration is over.
Or you will have to put iteration in the template.
The issue you’re facing is probably related to how you are handling the recursive calls in your
myFunction
. The recursive call is executed before the return statement, causing the function to exit prematurely. To fix this, you need to modify your code to properly handle the recursion and collect the results from all iterations.Here’s the try this version of your
myFunction
:Changes made:
result
string to accumulate the output for all iterations.result
string.This should work…. let me know.