Skip to content

Commit

Permalink
feat: init search engine feature ✨ #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Wivik committed May 31, 2023
1 parent cd13fa7 commit e73d014
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
70 changes: 69 additions & 1 deletion app.py
Expand Up @@ -68,7 +68,7 @@ def test_solutions_file(solutions_file):
def read_solutions(solutions_file):
"""load the whole content of the file and return it"""
with open(solutions_file, 'r') as file:
solutions_content = yaml.load(file, Loader=yaml.FullLoader)
solutions_content = yaml.safe_load(file)
return solutions_content

def get_tool_usage(search_tool, solutions):
Expand Down Expand Up @@ -96,6 +96,64 @@ def get_tool_usage(search_tool, solutions):
})
return tool_usage

def search(data, query):
results = []
search_result = handle_search(data, query)
print(search_result)
print(results)
return results

def handle_search(data, query, path=[]):
query = query.lower()
# print(f'query {query}')
if isinstance(data, list):
# print('isinstance data')
for index,item in enumerate(data):
path.append(str(index))
# print(f'str index ', str(index))
handle_search(item, query, path)
path.pop()
elif isinstance(data, dict):
for key, value in data.items():
# print(f'key ', key)
path.append(key)
print(f'path ', path)
# print(f'value ', value)
if key == 'name' and value.lower() == query:
print(f"Tool Found: {query}")
print("Related Use Cases:")
print(f'path -2' , path[:-2])
# for uc_path in path:
# print(f'uc path ', uc_path)
uc_data = get_data_by_path(data, path[:-2])
if uc_data:
print('uc_data')
print(uc_data['name'])
return uc_data['name']
elif isinstance(value, (list, dict)):
handle_search(value, query, path)
path.pop()


def get_data_by_path(data, path):
print(f'path ', path)
try:
for key in path:
print(f'key ', key)
if isinstance(data, dict):
print('isinstance dict')
data = data.get(key)
print(f'data : ', data)
elif isinstance(data, list):
print('isinstance list')
index = int(key)
data = data[index]
print(f'data ', data)
return data
except (KeyError, IndexError):
print('no data')
return None

## render main template

@app.route("/")
Expand Down Expand Up @@ -176,6 +234,16 @@ def custom_static(folder, filename):
## if not, we fallback on static dir
return send_from_directory(app.static_folder, os.path.join(folder, filename))

## search engine
@app.route("/search", methods=['POST'])
def search_view():
query_data = request.form
query = query_data['query']
solutions = read_solutions(solutions_file)
results = search(solutions, query)
return render_template('search.html.j2', results=results)


if __name__ == '__main__':
if args.freeze_mode:
freezer.freeze()
Expand Down
22 changes: 20 additions & 2 deletions static/css/style.css
Expand Up @@ -59,16 +59,20 @@ header {
background-position: right top;
}

header > nav {
display: flex;
align-items: left;
}

header > nav a:link,
header > nav a:visited {
display: inline-block;
color: var(--main-menu-link-color);
border: 1px solid var(--main-menu-link-border-color);
background-color: var(--main-menu-link-bg-color);
text-decoration: none;
font-weight: bold;
padding: 0.5rem;
margin-left: 0.2 rem;
margin-left: 0.2rem;
}

header > nav a:hover {
Expand All @@ -79,6 +83,20 @@ header > h1 {
margin-top: 0;
}

header > nav > form > input {
padding: 0.5rem;
margin-left: 0.2rem;
font-size: 1.15rem;
}

header > nav > form > input[type=submit] {
background-color: var(--main-menu-link-bg-color);
color: var(--main-menu-link-color);
font-weight: bold;
border: 1px solid var(--main-menu-link-border-color);
margin: 0;
}

/* Legend */

#legend {
Expand Down
5 changes: 5 additions & 0 deletions templates/main_menu.html.j2
Expand Up @@ -8,4 +8,9 @@
<a href="{{ url_for('main', open='all') }}">Open all Boxes</a>
{% endif %}
<a href="{{ url_for('about') }}">About</a>
<form action="{{ url_for('search_view') }}" method="POST">
<input type="text" name="query" placeholder="Search a use case or a tool" />
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input type="submit" value="Search" />
</form>
</nav>
12 changes: 12 additions & 0 deletions templates/search.html.j2
@@ -0,0 +1,12 @@
{% extends 'base.html.j2' %}

{% block content %}

<div id="content">
search

{{ results }}

</div>

{% endblock %}

0 comments on commit e73d014

Please sign in to comment.