-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtips.py
67 lines (59 loc) · 2.22 KB
/
tips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# Microlog. Copyright (c) 2023 laffra, dcharbon. All rights reserved.
#
SEARCH_URLS = {
"Google": "https://www.google.com/search?q=",
"Brave": "https://search.brave.com/search?q=",
"DuckDuckGo": "https://duckduckgo.com/?va=v&t=ha&q=",
"Bing": "https://www.bing.com/search?q=",
"SourceGraph": "https://sourcegraph.com/search?q=lang:",
"StackOverflow": "https://stackoverflow.com/search?q=",
"HackerNews": "https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=",
"Twitter": "https://twitter.com/search?q=",
}
import js # type: ignore
import pyodide # type: ignore
import random
from microlog.models import CallSite
PROMPTS = [
"best practices",
"performance",
"how can I",
]
IGNORE_MODULES = set([
"",
"copy",
"__main__",
"examples",
])
class Tips():
def __init__(self, calls):
self.calls = calls
self.modules = list(set([call.callSite.name.split(".")[0] for call in calls]) - IGNORE_MODULES)
try:
random.shuffle(self.modules)
except:
pass # micropython has no shuffle
usage = f"This code is using the following modules: {', '.join(self.modules)}.<br><br>" if self.modules else ""
prompt = " AND ".join(random.choice(PROMPTS).split(" "))
js.jQuery("#tips").empty() \
.append(js.jQuery("<div>").html(f"""
{usage}
To improve best practices or performance of your code, consider these resources:
""")) \
.append(*[
self.createButton(provider, f"/microlog/images/logo-{provider.lower()}.png", f"{url}Python AND {' AND '.join(list(self.modules)[:2])} AND {prompt}")
for provider, url in SEARCH_URLS.items()
])
def createButton(self, provider, logo, url):
return (js.jQuery("<button>")
.addClass("tips-button")
.append(
js.jQuery("<img>").addClass("tips-logo").attr("src", f"/microlog/{logo}"),
js.jQuery("<span>").text(provider),
)
.click(pyodide.ffi.create_proxy(lambda event: self.open(url)))
)
def open(self, url):
print("Tips", self.modules, url)
js.window.open(url)