i want the code to wait for previous line to be done before continuing

2k Views Asked by At

hi i'm using streamlit to take input from user and using GoogleNews module i'm searching news related to the input text and storing them in a variable "result_0".. but i want the bellow steps to finish before continuing

googlenews.search(inputt.iloc[0,0])
googlenews.get_news(inputt.iloc[0,0])
result_0 = googlenews.page_at(1)

but in fact whats happening is that the system is going directly to the next line which is :

if len(result_0) == 0:

and its always true because result didn't get the chance to load the news from previous step i tried using time.sleep() function but i'm not sure how long does the step take since the number of news depends on the input text

1

There are 1 best solutions below

0
RoseGod On

The problem is how you are using the GoogleNews package. Here is the code to show news by term in a streamlit app using GoogleNews:

import streamlit as st
from GoogleNews import GoogleNews

# add app title
st.title('Google news')
st.markdown("""---""")

# get user input for seraching news
user_input = st.sidebar.text_input("Enter news term")

state = st.sidebar.button("Get News!")

if state:
    # get news
    googlenews = GoogleNews()
    googlenews.get_news(user_input)
    news = googlenews.results()
    if news:
        for i in news:            
            st.markdown(f"**{i['title']}**")
            st.write(f"Published Date - {i['date']}")
            st.markdown(f"[Article Link]({i['link']})")
            st.markdown("---")
            
    else:
        st.write("No news for this term")

Here is the output:

enter image description here

Also I would suggest you use the GNews package instead because it has better functionality. Here is the code to show news by term in a streamlit app using GNews:

import streamlit as st
from gnews import GNews

# add app title
st.title('Google news')
st.markdown("""---""")

# get user input for seraching news
user_input = st.sidebar.text_input("Enter news term")

state = st.sidebar.button("Get News!")

if state:
    # get news
    news = GNews().get_news(user_input)
    if news:
        for i in news:
            st.markdown(f"**{i['title']}**")
            st.write(f"Published Date - {i['published date']}")
            st.write(i["description"])
            st.markdown(f"[Article Link]({i['url']})")
            st.markdown("""---""")
    else:
        st.write("No news for this term")

Here is the output: enter image description here