-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplotly_code_scraper.py
More file actions
87 lines (70 loc) · 2.17 KB
/
plotly_code_scraper.py
File metadata and controls
87 lines (70 loc) · 2.17 KB
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
""" Useful code to collect Plotly chart codes automatically.
It's actually a Streamlit app.
To run it just run 'pipenv run streamlit run plotly_code_scraper.py'
"""
import random
from pathlib import Path
from typing import List
import requests
import streamlit as st
from bs4 import BeautifulSoup
st.title("Testing Plotly theming")
urls = [
"https://plotly.com/python/line-and-scatter",
"https://plotly.com/python/line-charts",
"https://plotly.com/python/bar-charts",
"https://plotly.com/python/dot-plots",
"https://plotly.com/python/pie-charts",
"https://plotly.com/python/bubble-charts",
]
SCRIPT_PREFIX = """import streamlit as st
import plotly.express as px
import plotly.graph_objects as go
"""
CODE_BLOCK_SUFFIX = """
tab1, tab2 = st.tabs(["Default theme", "Streamlit theme"])
with tab1:
st.plotly_chart(fig)
with tab2:
st.plotly_chart(fig, theme="streamlit")
"""
CODE_BLOCK_FORMATTER = """
@st.experimental_memo
def get_chart_{}():
{}
{}
try:
get_chart_{}()
except Exception as e:
st.exception(e)
"""
@st.experimental_memo
def get_charts_code(url: str) -> List:
html = requests.get(url).content
soup = BeautifulSoup(html, "html.parser")
all_pres = list(soup.find_all("pre"))
all_code_blocks = list()
for pre in all_pres:
code_block_raw = pre.get_text()
if "dash" in code_block_raw or "fig" not in code_block_raw:
continue
function_index = random.randint(1, 100_000)
code_block = CODE_BLOCK_FORMATTER.format(
function_index,
"\n ".join(
[row for row in code_block_raw.splitlines() if "fig.show" not in row]
),
"\n ".join(CODE_BLOCK_SUFFIX.splitlines()),
function_index,
)
all_code_blocks += [code_block]
return all_code_blocks
for url in urls:
charts_code = get_charts_code(url)
charts_category = url.split("/")[-1].replace("-", "_").title()
charts_file = Path("pages") / Path(f"{charts_category}.py")
if charts_file.exists():
charts_file.unlink()
with charts_file.open("w") as file:
file.write(SCRIPT_PREFIX)
file.write("\n".join(charts_code))