File size: 1,839 Bytes
9686a60
 
 
c346913
9686a60
5655ca6
 
 
ddbfd1a
5655ca6
 
 
 
 
 
ddbfd1a
5655ca6
 
 
 
 
 
 
 
c346913
45a4f17
dd5b33e
c346913
45a4f17
 
 
 
5655ca6
 
45a4f17
da90807
45a4f17
 
 
 
 
 
 
 
 
 
 
 
 
5655ca6
 
45a4f17
e0f3b84
 
45a4f17
 
 
 
 
 
5655ca6
9686a60
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
import streamlit as st
from requests_toolkit import  AirQualityQuery


st.markdown('''# Country Air Quality Ranking''')
with st.form(key='my form'):
    country = st.text_input(
        label='Country:',
        placeholder='''e.g. "china"''',
        # label_visibility = 'collapsed'
    )

    # include_province = st.checkbox('search specific province')
    province = st.text_input(
        label='Province:',
        placeholder= f'''e.g. "fujian". (Leave blank for all provinces)''',
    )

    submit = st.form_submit_button('Search')

# print(dict(
#     country = country,
#     province = province,
# ))

if submit:
    st.markdown(f'''You entered: `{country}`, `{province}`''')

    if country != '':
        # all provinces
        if province == '':
            md_head = '''| ID | City |Province | US AQI |
| -------- | -------- | -------- | -------- |
'''
            md = st.empty()
            generator = AirQualityQuery.air_quality_by_country(country)
            while True:
                try:
                    with st.spinner():
                        i = next(generator)
                    new_md = ''
                    for id, j in enumerate(i):
                        new_md += f'''|{id + 1}|{j[0]}|{j[1]}|{j[2]}|\n'''
                    md.markdown(md_head + new_md)
                except StopIteration:
                    break
        # specific province
        else:
            md_head = '''| ID | City |Province | US AQI |
| -------- | -------- | -------- | -------- |
'''
            md = st.empty()
            with st.spinner():
                i = AirQualityQuery.air_quality_by_province_country(country, province)
            new_md = ''
            for id, j in enumerate(i):
                new_md += f'''|{id + 1}|{j[0]}|{j[1]}|{j[2]}|\n'''
            md.markdown(md_head + new_md)