-
Notifications
You must be signed in to change notification settings - Fork 1
/
scratchpad
93 lines (80 loc) · 3.04 KB
/
scratchpad
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
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
from weasyprint import HTML
# Example DataFrame
df = pd.DataFrame({
'date': pd.date_range(start='1/1/2022', periods=100),
'values': np.random.rand(100)
})
# Helper function to create a plot based on a given date
def create_plot(selected_date, df):
filtered_data = df[df['date'] == pd.to_datetime(selected_date)]
fig = px.line(filtered_data, x='date', y='values', title='Dynamics Over Time', labels={'values': 'Value'})
return fig
# Function to generate HTML content for PDF generation
def generate_html_content(date, fig):
html_content = f"""
<html>
<head>
<title>Summary Report</option>
</head>
<body>
<h1>Summary for {date}</h1>
{fig.to_html()}
<p>Insights:</p>
<ul>
<li>Note: This is a template text.</li>
<li>Insight: This insight changes based on the selected date.</li>
</ul>
</body>
</html>
"""
return html_content
# Helper function to simulate chatbot responses (this would be replaced by your actual chatbot logic)
def chatbot_response(data_type, date_range):
return f"Chatbot response based on {data_type} from {date_range[0]} to {date_range[1]}."
# Streamlit app
def main():
st.title("Streamlit App with Two Tabs")
# Initialize session state for conversation history
if 'conversation' not in st.session_state:
st.session_state['conversation'] = []
# Creating tabs
tab1, tab2 = st.tabs(["Summary View", "Chatbot"])
with tab1:
st.header("Summary View")
# Date dropdown
date = st.selectbox('Select Date', df['date'].dt.date.unique())
# Display the dynamic plot based on the selected date
plot_button = st.button('Show Plot')
if plot_button:
fig = create_plot(date, df)
st.plotly_chart(fig, use_container_width=True)
save_pdf = st.button("Save as PDF")
if save_pdf and plot_button: # Ensure plot was generated
html_content = generate_html_content(date, fig)
html = HTML(string=html_content)
pdf = html.write_pdf()
st.download_button(
label="Download PDF",
data=pdf,
file_name=f"summary_{date}.pdf",
mime="application/pdf"
)
with tab2:
st.header("Chatbot Interface")
# Drop downs for chatbot
data_type = st.selectbox('Select Data Type', ['Type 1', 'Type 2', 'Type 3'])
date_range = st.date_input("Select Date Range", [])
# Start/Continue Conversation
if st.button('Start Conversation'):
response = chatbot-reponse(data_type, date_range)
st.session_state.conversation.append("User: " + f"{data_type} from {date_range[0]} to {date_range[1]}")
st.session_state.conversation.append("Bot: " + response)
# Display conversation history
for message in st.session_state.conversation:
st.text(message)
if __name__ == "__main__":
main()