forked from streamlit/demo-self-driving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter4.py
116 lines (106 loc) · 4.12 KB
/
scatter4.py
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
112
113
114
115
116
#run from app.py this has additional data for combined keyword and search volume across portals. However, is not working.
import streamlit as st
import streamlit.components.v1 as components
import numpy as np
import matplotlib.pyplot as plt
import os
import sys
import pandas as pd
import plotly.express as px
#GRAPH WITHOUT LABELS----------------------------------------------
#keyword filtering
message = st.empty()
def filterterm(df, scatterterm, scattersearch):
if scatterterm == 'no':
message.text("Select a visualization and enter a search term.")
dff = df
#st.dataframe(dff)
elif scattersearch=='volume':
dff = df.loc[(df['Keyword'].str.contains(scatterterm))]
# changing the Volume column from text to numeric so we can sort and use a color scale
dff['Volume'] = pd.to_numeric(dff['Volume'])
elif scattersearch=='page':
dff = df.loc[(df['Page'].str.contains(scatterterm))]
#add a new column with the text for hover
dff['CustomerSearch'] = df['Keyword'] + "<br>Page: " + df['Page']
#st.dataframe(dff)
else:
dff = df.loc[(df['Keyword'].str.contains(scatterterm))]
dff['CustomerSearch'] = df['Keyword'] + "<br>Page: " + df['Page']
# if the term has no results, tell them and use the full data frame
return dff
def matscatterplot3(scatterterm, scattersearch):
if len(scatterterm) == 0:
scatterterm = 'no'
#GRAPH WITHOUT LABELS----------------------------------------------
scattertype = st.sidebar.radio(
"Select a Visualization",
('Blended Rank', 'Blended Rank Change', 'Combined Keyword'))
# Note Plotly express can't be included directly in streamlit, you have to render it as an html page and then read it in
# the same way you do with networkx visualizations.
plt.style.use('seaborn-whitegrid')
#upload main data for blended rank and blended rank change viz
dfbr = pd.read_csv("https://raw.githubusercontent.com/tyrin/info-topo-dash/master/data/TotalOrganicKeywords-Jan2021vsJan2022.csv")
dff = filterterm(dfbr, scatterterm, scattersearch)
termresults = "yes"
if dff.isnull().values.any() or scatterterm == "no":
message.text("No results for your term. Check the data below to find a valid keyword.")
st.dataframe(dfbr)
termresults = "no"
elif scatterterm != "no" or termresults !="no":
#st.write("Your term is " + scatterterm)
if scattertype == "Blended Rank":
fig = px.scatter(dff, x="Blended Rank", y="Search Volume",
text="CustomerSearch",
log_x=True,
size="Blended Rank",
color="Blended Rank",
size_max=25)
#fig.update_traces(textposition='top center')
fig.update_traces(mode="markers")
fig.update_layout(
height=800,
title_text='Blended Rank and Search Volume'
)
elif scattertype == "Blended Rank Change":
fig = px.scatter(dff, x="Blended Rank Change", y="Search Volume",
text="CustomerSearch",
log_x=False,
error_x_minus="Blended Rank Change",
size="Blended Rank",
color="Blended Rank",
size_max=25)
#fig.update_traces(textposition='top center')
fig.update_traces(mode="markers")
fig.update_layout(
height=800,
title_text='Blended Rank Change and Search Volume'
)
elif scattertype == "Combined Keyword":
dfck = pd.read_csv("https://raw.githubusercontent.com/tyrin/info-topo-dash/master/data/combinedKeywords.csv")
dff = filterterm(dfck, scatterterm, 'volume')
fig = px.bar(dff, x="Keyword", y=dff['Volume'], color="Portal", title="Combined Keywords")
#Other variations of representation
#fig = px.bar(dff, x="Keyword", y=dff['Volume'].astype(int), color=dff['Volume'].astype(int), title="Combined Keywords")
#fig = px.bar(df1, x=df1.time, y=df2.market, color=df1.sales)
#if scatterterm != "no" or termresults !="no"
fig.write_html("scatter.html")
HtmlFile = open("scatter.html", 'r', encoding='utf-8')
source_code = HtmlFile.read()
components.html(source_code, height = 700,width=900)
st.dataframe(dff)
@st.cache
def convert_df(dff):
return dff.to_csv().encode('utf-8')
csv = convert_df(dff)
st.download_button(
"Press to Download",
csv,
"file.csv",
"text/csv",
key='download-csv'
)
def noresults(df):
st.dataframe(df)
return df
#fig2.write_html("scatter.html")