From a688e41fb0067821db84d6d48eee1dbf437ec772 Mon Sep 17 00:00:00 2001 From: khuyentran1401 Date: Fri, 3 Sep 2021 12:53:47 -0500 Subject: [PATCH] add pywebio --- applications/bore_app/extract_books.py | 39 --------------- .../{bore_app/main.py => bored_app.py} | 50 ++++++++++++++++--- 2 files changed, 44 insertions(+), 45 deletions(-) delete mode 100644 applications/bore_app/extract_books.py rename applications/{bore_app/main.py => bored_app.py} (56%) diff --git a/applications/bore_app/extract_books.py b/applications/bore_app/extract_books.py deleted file mode 100644 index 1d0b57f..0000000 --- a/applications/bore_app/extract_books.py +++ /dev/null @@ -1,39 +0,0 @@ -import requests -import spacy -from rich import print - -def extract_noun_phrases(text: str): - """Extract noun phrases from a text""" - nlp = spacy.load("en_core_web_sm") - doc = nlp(text) - return [chunk.text for chunk in doc.noun_chunks] - -def get_query(phrase: str): - """Turn noun phrase into a query by replacing space with +""" - return '+'.join(phrase.split(' ')) - -def get_query_for_noun_phrases(text: str): - """Turn list of noun phrases into a list of queries""" - noun_phrases = extract_noun_phrases(text) - return [get_query(phrase) for phrase in noun_phrases] - -def get_books(query: str): - """Get the first 3 books based on the query""" - api = f"https://openlibrary.org/search.json?title={query}" - response = requests.get(api) - content = response.json()['docs'][:3] - return content - -def get_books_of_text(text: str): - """Get books given a test""" - queries = get_query_for_noun_phrases(text) - books = [] - for query in queries: - books.extend(get_books(query)) - return books - -if __name__ == '__main__': - text = 'Write a thank you letter to an influential person in your life' - - books = get_books_of_text(text) - print(books[0]) \ No newline at end of file diff --git a/applications/bore_app/main.py b/applications/bored_app.py similarity index 56% rename from applications/bore_app/main.py rename to applications/bored_app.py index ddabf51..d8ea8b0 100644 --- a/applications/bore_app/main.py +++ b/applications/bored_app.py @@ -1,11 +1,47 @@ +from pywebio import * +from pywebio.output import * +from pywebio.input import * +from pywebio.pin import * + import requests -from pywebio import start_server -from pywebio.output import put_markdown, put_table, put_loading, use_scope -from pywebio.pin import pin_wait_change, put_select -from extract_books import get_books_of_text from typing import List +import requests +import spacy + +# ---- extract_books util functions +def extract_noun_phrases(text: str): + """Extract noun phrases from a text""" + nlp = spacy.load("en_core_web_sm") + doc = nlp(text) + return [chunk.text for chunk in doc.noun_chunks] + +def get_query(phrase: str): + """Turn noun phrase into a query by replacing space with +""" + return '+'.join(phrase.split(' ')) + +def get_query_for_noun_phrases(text: str): + """Turn list of noun phrases into a list of queries""" + noun_phrases = extract_noun_phrases(text) + return [get_query(phrase) for phrase in noun_phrases] + +def get_books(query: str): + """Get the first 3 books based on the query""" + api = f"https://openlibrary.org/search.json?title={query}" + response = requests.get(api) + content = response.json()['docs'][:3] + return content + +def get_books_of_text(text: str): + """Get books given a test""" + queries = get_query_for_noun_phrases(text) + books = [] + for query in queries: + books.extend(get_books(query)) + return books + +# ---- bore API util function def get_activity_content(inputs: dict): """Get a random activity using Bored API""" if inputs['value'] == 'random': @@ -16,6 +52,7 @@ def get_activity_content(inputs: dict): content = response.json() return content +# ---- layout util functions def display_activity_for_boredom(): put_markdown("# Find things to do when you're bored") activity_types = ['random', "education", "recreational", "social", @@ -32,7 +69,8 @@ def create_book_table(books: List[dict]): put_table(book_table) -def app(): +# ---- main func running as the web app on pyweb.io FaaS platform +def main(): display_activity_for_boredom() while True: @@ -52,4 +90,4 @@ def app(): if __name__ == '__main__': - start_server(app, port=36635, debug=True) + start_server(main, port=36635, debug=True)