Skip to content

Commit

Permalink
Make Bing search async with separate search_page
Browse files Browse the repository at this point in the history
  • Loading branch information
parradam committed Jan 19, 2025
1 parent e786dfe commit 9322479
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ ENV/
env.bak/
venv.bak/

# VS Code config
.vscode/

# Spyder project settings
.spyderproject
.spyproject
Expand Down
39 changes: 32 additions & 7 deletions lute/bing/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@
import os
import re
import urllib.request
from flask import Blueprint, request, Response, render_template, jsonify, current_app
from flask import (
Blueprint,
request,
Response,
render_template,
jsonify,
current_app,
url_for,
)


bp = Blueprint("bing", __name__, url_prefix="/bing")


@bp.route(
"/search_page/<int:langid>/<string:text>/<string:searchstring>", methods=["GET"]
)
def bing_search_page(langid, text, searchstring):
"Load search page."

# Create URL for bing_search and pass into template.
search_url = url_for(
"bing.bing_search", langid=langid, text=text, searchstring=searchstring
)

return render_template(
"imagesearch/index.html", langid=langid, text=text, search_url=search_url
)


@bp.route("/search/<int:langid>/<string:text>/<string:searchstring>", methods=["GET"])
def bing_search(langid, text, searchstring):
"Do an image search."
Expand Down Expand Up @@ -60,12 +84,13 @@ def build_struct(image):
# Also bing seems to throttle images if the count is higher (??).
images = images[:25]

return render_template(
"imagesearch/index.html",
langid=langid,
text=text,
images=images,
error_message=error_msg,
return jsonify(
{
"langid": langid,
"text": text,
"images": images,
"error_message": error_msg,
}
)


Expand Down
2 changes: 1 addition & 1 deletion lute/static/js/dict-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ImageLookupButton extends GeneralLookupButton {

const raw_bing_url = 'https://www.bing.com/images/search?q=[LUTE]&form=HDRSC2&first=1&tsc=ImageHoverTitle';
const binghash = raw_bing_url.replace('https://www.bing.com/images/search?', '');
const url = `/bing/search/${LookupButton.LANG_ID}/${encodeURIComponent(use_text)}/${encodeURIComponent(binghash)}`;
const url = `/bing/search_page/${LookupButton.LANG_ID}/${encodeURIComponent(use_text)}/${encodeURIComponent(binghash)}`;

iframe.setAttribute("src", url);
}; // end handler
Expand Down
60 changes: 45 additions & 15 deletions lute/templates/imagesearch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,10 @@
then paste from your clipboard.
</p>

{% for image in images %}
<span class="initial"
style="margin: 2px; display:inline-block;"
onmouseover="highlight_image(this);"
onmouseout="un_highlight_image(this);"
onclick='save_image_locally("{{ image['src'] }}", {{ langid }}, "{{ text }}")'
>
{{ image['html'] | safe }}
</span>
{% endfor %}

{% if error_message != "" %}
<p><i>Error contacting image server: {{ error_message }}</i></p>
{% endif %}
</div>
</body>

<script>

let update_term_form = function(filename) {
// Well, this took **far** too long to figure out ...
let fr = window.parent.frames['wordframe'];
Expand Down Expand Up @@ -146,6 +131,51 @@

});

$(document).ready(function() {
const searchUrl = '{{ search_url }}';
const container = document.getElementById('termimagesearch');

$.ajax({
url: searchUrl,
type: 'GET',
dataType: 'json',
success: function(data) {
if (data.images.length === 0) {
const p = document.createElement('p');
p.textContent = 'No images found.';
container.appendChild(p);
return;
}

// Make Bing API call for images asynchronously
// This avoids blocking the page load
data.images.forEach(function(item) {
const imgWrapper = create_imageWrapper_element(item.src, data.langid, data.text);
container.appendChild(imgWrapper);
});
},
error: function(xhr, status, error) {
console.error(`Error ${error}; ${status}; ${xhr.responseText}`);
}
});
});

function create_imageWrapper_element(src, langid, text) {
const imgWrapper = document.createElement('span');
imgWrapper.classList.add('initial');
imgWrapper.style.margin = '2px';
imgWrapper.style.display = 'inline-block';
imgWrapper.onmouseover = () => highlight_image(imgWrapper);
imgWrapper.onmouseout = () => un_highlight_image(imgWrapper);
imgWrapper.onclick = () => save_image_locally(src, langid, text);

const img = document.createElement('img');
img.src = src;

imgWrapper.appendChild(img);

return imgWrapper;
}
</script>

</html>

0 comments on commit 9322479

Please sign in to comment.