-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from oss-slu/fixing_backend_API.py
Disabled flask functionality temporarily and added arguments for the data retrieval.
- Loading branch information
Showing
4 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,5 +106,4 @@ def find_sections(): | |
return 'OK' | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) | ||
|
||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from flask import Flask, request | ||
from docx import Document | ||
from pathlib import Path | ||
import sys | ||
|
||
#@app.route('/find-sections', methods=['POST']) | ||
def find_sections(): | ||
# Check for missing request data | ||
""" | ||
if 'file_path' not in request.json: | ||
return 'Missing file_path field', 400 | ||
if 'search_terms' not in request.json: | ||
return 'Missing search_terms field', 400 | ||
if 'sections' not in request.json: | ||
return 'Missing sections field', 400 | ||
if 'specifyLines' not in request.json: | ||
return 'Missing specifyLines field', 400 | ||
if 'use_total_lines' not in request.json: | ||
return 'Missing use_total_lines field', 400 | ||
if 'lines' not in request.json: | ||
return 'Missing lines field', 400 | ||
""" | ||
|
||
# Get the file path from the request | ||
|
||
file_path = sys.argv[1] | ||
tempSearch_terms = sys.argv[2] | ||
tempSections = sys.argv[3] | ||
tempSpecifyLines = sys.argv[4] | ||
tempUse_total_lines = sys.argv[5] | ||
tempTotal_lines = sys.argv[6] | ||
|
||
|
||
search_terms = [tempSearch_terms] | ||
use_total_lines = bool(tempUse_total_lines) | ||
total_lines = int(tempTotal_lines) | ||
|
||
tempSections = list(tempSections) | ||
sections = [int(val) for val in tempSections] | ||
|
||
specifyLines = [] | ||
num = 0 | ||
while num <= len(sections): | ||
specifyLines.append(tempSpecifyLines) | ||
num += num + 1 | ||
|
||
|
||
"""file_path = request.json['file_path']""" | ||
|
||
# Read the file | ||
with open(file_path, 'r') as f: | ||
Lines = f.readlines() | ||
|
||
# Get the search terms, sections, and lines from the request | ||
""" | ||
search_terms = request.json['search_terms'] | ||
sections = request.json['sections'] | ||
specifyLines = request.json['specifyLines'] | ||
use_total_lines = request.json['use_total_lines'] | ||
total_lines = request.json['lines'] | ||
""" | ||
|
||
# Create a new document | ||
document = Document() | ||
|
||
# Iterate over the search terms and find the corresponding sections | ||
for term in search_terms: | ||
lineNo = 0 | ||
termLineNo = [] | ||
termsNum = 0 | ||
for line in Lines: | ||
if term in line: | ||
termLineNo.append(lineNo) | ||
termsNum += 1 | ||
#print(termsNum, term) | ||
lineNo += 1 | ||
|
||
|
||
|
||
# Add the sections to the document | ||
|
||
|
||
for i in sections: | ||
section_lines = specifyLines[i-1].split() | ||
start_line = termLineNo[i-1] | ||
line_empty = 0 | ||
|
||
if section_lines[0] == 'WHOLE' and use_total_lines == False: | ||
while line_empty == 0: | ||
if Lines[start_line] != "\n": | ||
section = document.add_paragraph(Lines[start_line]) | ||
start_line += 1 | ||
else: | ||
line_empty = 1 | ||
|
||
if section_lines[0] == 'WHOLE' and use_total_lines == True: | ||
for _ in range(total_lines - start_line + termLineNo[i-1]): | ||
section = document.add_paragraph(Lines[start_line]) | ||
start_line += 1 | ||
line_empty = 1 | ||
else: | ||
start_line += 1 | ||
line_empty = 1 | ||
|
||
elif section_lines[0] == 'FIRST': | ||
line_count = -1 | ||
while line_count < int(section_lines[1]): | ||
section = document.add_paragraph(Lines[start_line]) | ||
start_line += 1 | ||
line_count += 1 | ||
|
||
elif section_lines[0] == 'LAST': | ||
line_count = -1 | ||
document.add_paragraph(Lines[start_line]) | ||
document.add_paragraph(Lines[start_line + 1]) | ||
while line_count < int(section_lines[1]): | ||
section = document.add_paragraph(Lines[start_line+10]) | ||
start_line += 1 | ||
line_count += 1 | ||
|
||
elif section_lines[0] == 'SPECIFIC': | ||
specific_lines = [int(l) for l in section_lines[1].split(",")] | ||
document.add_paragraph(Lines[start_line]) | ||
for l in specific_lines: | ||
section = document.add_paragraph(Lines[start_line + l + 1]) | ||
|
||
|
||
|
||
|
||
try: | ||
# Save the document | ||
final_location = Path("../../../esp/outputData/data_conversion.docx") | ||
final_location = final_location.resolve() | ||
document.save(final_location) | ||
except Exception as e: | ||
return f'Error saving document: {e}', 500 | ||
|
||
return 'OK' | ||
|
||
|
||
|
||
""" | ||
if __name__ == '__main__': | ||
app.run(debug=True) | ||
""" | ||
|
||
|
||
result = find_sections() | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
In regards to the arguments, reference the sample line below: | ||
|
||
python APIusingCMD.py "C:\Users\seala\csci4961\ORCAdata\Styrene-bd2.txt" "CARTESIAN COORDINATES (ANGSTROEM)" 123 "FIRST 6" True 2000 | ||
|
||
To start, you must call python APIusingCMD.py. The remaining arguments represent each component one needs to generate | ||
the output file. | ||
|
||
Arguments: | ||
|
||
0 - name of python file (APIusingCMD.py) | ||
1 - The dir of the input file location | ||
2 - The key terms one desires **note: key terms must be in all caps and within quotes (""), this argument is sensitive to caps and spaces. | ||
3 - The list of sections, pass this as a string with no spaces (i.e. 123 will turn into a list [1,2,3] of ints | ||
4 - represents the specify lines element, This must be a string, such as "WHOLE", "FIRST", "LAST" or "SPECIFIC", where for first and last, you must specify a number. for instance, "FIRST 6" (**note the space and the use of all caps) stands for the first six lines of that section. | ||
5 - A bool value to use the total lines passed in the last argument or not. Pass this as a string either True or False. | ||
6 - total number of lines for the output doc. pass this as a string such as 2000 |
Binary file not shown.