File size: 2,751 Bytes
1aa7583
 
 
 
 
 
 
 
1c76a96
 
 
 
 
 
cc8b36c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c76a96
 
263f645
 
 
1aa7583
1c76a96
 
1aa7583
e338567
1c76a96
1aa7583
1c76a96
1aa7583
 
1c76a96
656f25c
 
 
 
 
 
 
 
 
d1dca6b
656f25c
1aa7583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
656f25c
1aa7583
 
 
 
 
 
 
 
 
656f25c
d1dca6b
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import ibis
from ibis import _
import streamlit as st

st.set_page_config(layout="wide",
                   page_title="TPL LandVote",
                   page_icon=":globe:")

'''
# LandVote Prototype

'''


## Chatbot
import os
import pandas as pd
import matplotlib.pyplot as plt
from pandasai.llm.openai import OpenAI
from pandasai import Agent
from pandasai.responses.streamlit_response import StreamlitResponse

llm = OpenAI(api_token=st.secrets["OPENAI_API_KEY"])
df1 = pd.read_csv("data.csv")

agent = Agent(
    [df1],
    config={"verbose": True, "response_parser": StreamlitResponse, "llm": llm},
)



with st.sidebar:

    '''
    ## Data Assistant (experimental)

    Ask questions about the landvote data, like:

    - What are the top states for approved conservation funds?
    - Plot the total funds spent in conservation each year.
    - What city has approved the most funds in a single measure? What was the description of that vote?
    - Which state has had largest number measures fail? What is that as a fraction of it's total measures?
    '''
    
    prompt = st.chat_input("Ask about the data")
    if prompt:
        with st.spinner():
            resp = agent.chat(prompt)
            if os.path.isfile('exports/charts/temp_chart.png'):
                im = plt.imread('exports/charts/temp_chart.png')
                st.image(im)
                os.remove('exports/charts/temp_chart.png')
            st.write(resp)
        


# year = st.slider("Select a year", min_value=1988, max_value=2024, value=2022, step=2)
year = st.slider("Select a year", min_value=1988, max_value=2024, value=2022, step=1)

#gdf = df.filter(_.year==year).execute()


import leafmap.maplibregl as leafmap
m = leafmap.Map(style="positron",   center=(-100, 30), zoom=5)

url = "https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/vote.pmtiles"

#gdf = df.filter(_.year==1988).execute()
#gdf.to_file("vote.geojson")

outcome = [
      'match',
      ['get', 'Status'], 
      "Pass", '#2E865F',
      "Fail", '#FF3300', 
      '#ccc'
    ]
paint = {"fill-extrusion-color": outcome, 
         "fill-extrusion-opacity": 0.7,
         "fill-extrusion-height": ["*", ["get", "log_amount"], 5000],
        }
style = {
    "layers": [
        {
            "id": "votes",
            "source": "vote",
            "source-layer": "vote",
            "type": "fill-extrusion",
            "filter": [
                "==",
                ["get", "year"],
                year,
            ],  # only show buildings with height info
            "paint": paint
        },
    ],
}

m.add_pmtiles(
    url,
    style=style,
    visible=True,
    opacity=1.0,
    tooltip=True,
    fit_bounds=True,
)
#m.add_layer_control()
m.to_streamlit()