Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import altair as alt
|
| 4 |
import datetime
|
| 5 |
import re
|
| 6 |
import os
|
| 7 |
import asyncio
|
| 8 |
import aiohttp
|
|
|
|
| 9 |
|
| 10 |
from dateutil.relativedelta import relativedelta
|
| 11 |
|
|
@@ -77,7 +77,6 @@ st.sidebar.title("Filters")
|
|
| 77 |
with st.sidebar.expander("Parameters", expanded=True):
|
| 78 |
default_start_date = datetime.date.today() - relativedelta(months=2)
|
| 79 |
start_date = st.date_input("Start transaction date", value=default_start_date)
|
| 80 |
-
#start_date = st.date_input("Start transaction date", value=datetime.date(2025, 1, 1))
|
| 81 |
top_n = st.slider("Top N stocks", min_value=1, max_value=20, value=10,
|
| 82 |
help="Select the top N stock by trade amount and volume.")
|
| 83 |
|
|
@@ -158,21 +157,36 @@ if run_button:
|
|
| 158 |
.groupby(["ticker", "chamber", "tradeType"], as_index=False)
|
| 159 |
.agg({"amount": "sum", "count": "sum"})
|
| 160 |
)
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
)
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
chart = alt.layer(bars, text).properties(width=40 * len(top_tickers), height=400)
|
| 170 |
-
st.altair_chart(chart, use_container_width=True)
|
| 171 |
|
| 172 |
# Reorder Senate columns
|
| 173 |
if not senate_data.empty:
|
| 174 |
desired_order_senate = [
|
| 175 |
-
"office",
|
| 176 |
"dateRecieved",
|
| 177 |
"symbol",
|
| 178 |
"type",
|
|
@@ -187,7 +201,7 @@ if run_button:
|
|
| 187 |
# Reorder House columns
|
| 188 |
if not house_data.empty:
|
| 189 |
desired_order_house = [
|
| 190 |
-
"representative",
|
| 191 |
"disclosureDate",
|
| 192 |
"ticker",
|
| 193 |
"type",
|
|
@@ -199,13 +213,15 @@ if run_button:
|
|
| 199 |
reordered_house_cols = existing_house_cols + remaining_house_cols
|
| 200 |
house_data = house_data[reordered_house_cols]
|
| 201 |
|
| 202 |
-
st.
|
| 203 |
-
|
| 204 |
-
|
|
|
|
| 205 |
|
| 206 |
-
st.
|
| 207 |
-
|
| 208 |
-
|
|
|
|
| 209 |
else:
|
| 210 |
st.write("Set filters and press Run to load data.")
|
| 211 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import datetime
|
| 4 |
import re
|
| 5 |
import os
|
| 6 |
import asyncio
|
| 7 |
import aiohttp
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
|
| 10 |
from dateutil.relativedelta import relativedelta
|
| 11 |
|
|
|
|
| 77 |
with st.sidebar.expander("Parameters", expanded=True):
|
| 78 |
default_start_date = datetime.date.today() - relativedelta(months=2)
|
| 79 |
start_date = st.date_input("Start transaction date", value=default_start_date)
|
|
|
|
| 80 |
top_n = st.slider("Top N stocks", min_value=1, max_value=20, value=10,
|
| 81 |
help="Select the top N stock by trade amount and volume.")
|
| 82 |
|
|
|
|
| 157 |
.groupby(["ticker", "chamber", "tradeType"], as_index=False)
|
| 158 |
.agg({"amount": "sum", "count": "sum"})
|
| 159 |
)
|
| 160 |
+
# Create Plotly figure
|
| 161 |
+
fig = go.Figure()
|
| 162 |
+
for chamber in chart_data["chamber"].unique():
|
| 163 |
+
for trade in ["purchase", "sale"]:
|
| 164 |
+
df_subset = chart_data[(chart_data["chamber"] == chamber) & (chart_data["tradeType"] == trade)]
|
| 165 |
+
if not df_subset.empty:
|
| 166 |
+
color = "green" if trade == "purchase" else "red"
|
| 167 |
+
fig.add_trace(go.Bar(
|
| 168 |
+
x=df_subset["ticker"],
|
| 169 |
+
y=df_subset["amount"],
|
| 170 |
+
text=df_subset["count"],
|
| 171 |
+
textposition="auto",
|
| 172 |
+
name=f"{chamber} {trade}",
|
| 173 |
+
offsetgroup=chamber,
|
| 174 |
+
marker_color=color
|
| 175 |
+
))
|
| 176 |
+
fig.update_layout(
|
| 177 |
+
barmode="stack",
|
| 178 |
+
xaxis_tickangle=-45,
|
| 179 |
+
width=40 * len(top_tickers),
|
| 180 |
+
height=400,
|
| 181 |
+
title="Total Amount per Ticker"
|
| 182 |
)
|
| 183 |
+
with st.container(border=True):
|
| 184 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
| 185 |
|
| 186 |
# Reorder Senate columns
|
| 187 |
if not senate_data.empty:
|
| 188 |
desired_order_senate = [
|
| 189 |
+
"office",
|
| 190 |
"dateRecieved",
|
| 191 |
"symbol",
|
| 192 |
"type",
|
|
|
|
| 201 |
# Reorder House columns
|
| 202 |
if not house_data.empty:
|
| 203 |
desired_order_house = [
|
| 204 |
+
"representative",
|
| 205 |
"disclosureDate",
|
| 206 |
"ticker",
|
| 207 |
"type",
|
|
|
|
| 213 |
reordered_house_cols = existing_house_cols + remaining_house_cols
|
| 214 |
house_data = house_data[reordered_house_cols]
|
| 215 |
|
| 216 |
+
with st.container(border=True):
|
| 217 |
+
st.subheader("Senate Data")
|
| 218 |
+
st.write("Latest Transaction in Senate. Please sort the table by **`disclosureDate`** and/or **`dateRecieved`** columns.")
|
| 219 |
+
st.dataframe(senate_data, use_container_width=True)
|
| 220 |
|
| 221 |
+
with st.container(border=True):
|
| 222 |
+
st.subheader("House Data")
|
| 223 |
+
st.write("Latest Transaction in House. Please sort the table by **`disclosureDate`** and/or **`transactionDate`** columns.")
|
| 224 |
+
st.dataframe(house_data, use_container_width=True)
|
| 225 |
else:
|
| 226 |
st.write("Set filters and press Run to load data.")
|
| 227 |
|