Skip to content

Commit

Permalink
add pywebio
Browse files Browse the repository at this point in the history
  • Loading branch information
khuyentran1401 committed Sep 3, 2021
1 parent 5aabe6e commit a688e41
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
39 changes: 0 additions & 39 deletions applications/bore_app/extract_books.py

This file was deleted.

50 changes: 44 additions & 6 deletions applications/bore_app/main.py → applications/bored_app.py
Original file line number Diff line number Diff line change
@@ -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':
Expand All @@ -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",
Expand All @@ -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:
Expand All @@ -52,4 +90,4 @@ def app():


if __name__ == '__main__':
start_server(app, port=36635, debug=True)
start_server(main, port=36635, debug=True)

0 comments on commit a688e41

Please sign in to comment.