-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_outputs.py
198 lines (130 loc) · 6.88 KB
/
py_outputs.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from sierra import *
title('Documentation | Sierra - 2.0.0')
# writeWA("\n"r'<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">')
# writeWA("\n"r'<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>')
# writeWA("\n"r'<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>')
# CSS
with cTags('p') as p_tag:
p_tag.css(font_family="'Ubuntu', sans-serif;", line_height='28px')
with cTags('pg_title') as pg_title:
pg_title.css(font_family="'Signika Negative', sans-serif", font_size='25px', margin_top='30px', display='block')
writeCSS('pre', {"background-color": "whitesmoke", "margin-left": "0.1%"})
# CONTENT
addFont("https://fonts.googleapis.com/css2?family=Titillium+Web&display=swap")
addFont("https://fonts.googleapis.com/css2?family=Ubuntu&display=swap")
addFont("https://fonts.googleapis.com/css2?family=Signika+Negative&display=swap")
addFont("https://fonts.googleapis.com/css2?family=Oswald&display=swap")
addFont("https://fonts.googleapis.com/css2?family=Noto+Sans+KR&display=swap")
addFont("https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@300&display=swap")
with image('white_sierra.JPG', attr='href="https://github.com/BrainStormYourWayIn/sierra" alt="Sierra"') as i:
i.show()
i.css(margin_left='35%')
head('Documentation - Sierra v2.0.0', type='h1', font_family="'Titillium Web', sans-serif",
color="#1d37e0")
openBody()
with div(div_class='python_out'):
with open_tag('pg_title'):
writeWA('Working with Python Loops, Functions, Variables and Conditions')
p(r'''Displaying Python outputs on your web applications are actually pretty simple with Sierra.
To work with this, you can use <code>writeWA()</code> or <code>p()</code> , which has been covered on the main page of the
documentation. You can print variables, output functions, use loops, conditions or anything.''')
p(f'''Say you've loaded in a .csv file with pandas and write some quick code for displaying
each row as a list: ''')
with open_tag('pre'):
writeWA(r'''
import pandas as pd
from sierra import *
df = pd.read_csv('path/to/the/file.csv')
df
this is a csv
0 word word1 word2 word3
1 test1 test2 test3 test4
2 foo1 foo2 foo3 foo4
for i in range(0, 3):
for g in range(1, 4):
if i+1==g:
a = f'r{g} = {list(df.iloc[i])}{br}' # var 'br' stands for <br> See 'Other Functions'
writeWA(a)
# or if you want it on a paragraph
p(a)
# Output on the web application:
# r1 = ['word', 'word1', 'word2', 'word3']
# r2 = ['test1', 'test2', 'test3', 'test4']
# r3 = ['foo1', 'foo2', 'foo3', 'foo4']
''')
with div(div_class='img-map'):
p(f'''Say you want to create an image map, with which you can perform different actions by clicking on
shape and subsequently coordinate-secified parts of the shape on the image. {br}
See <a href="https://www.w3schools.com/html/html_images_imagemap.asp" target='blank'>image map</a>.
You can use f-strings and <code>for loop</code> to make it easy: ''')
with open_tag('pre'):
writeWA(r'''
with image(src='workplace.jpg', attr="usemap='#workmap'") as i:
i.show()
with open_tag('map', attr='name="workmap"'):
shape = ['rect', 'rect', 'circle']
coords = ["34,44,270,350", "290,172,333,250", "337,300,44"]
alt = ['Computer', 'Phone', 'Coffee']
href = ['computer.htm', 'phone.htm', 'coffee.htm']
for shape, coord, alt, href in zip(shapes, coords, alt, href):
with open_tag('area', attr=f'shape="{shape}" coord="{coord}" alt="{alt}" href="{href}"'):
pass
autoPrettify()
''')
p(f'''Here, you first display the image and give it an attribute <code>usemap</code>, then you enter in four lists
that contain the attributes of the three areas to be covered, open the <map> tag and map it to the
attribute <code>usemap</code> given to the image. Then you do a simple for loop and
unpack each item in the lists with <code>zip()</code>, open the <area> tag and simply use
f-strings to enter in four different attributes to three different <area> tags which all come under <map>!
So instead of manually entering in every single tag and attribute, you've used Python's <code>list</code>
and <code>for loop</code> to get the job done.''')
with div(div_class='funcs'):
p(f'''With Sierra, using and displaying outputs of Python functions on your web application
is made easy as cake''')
p(f'''Here's an example of scraping a webpage with requests, given it's URL, and displaying
all text within the <p> tag: ''')
with open_tag('pre'):
writeWA(r'''
from sierra import *
import re
import urllib3
def ExtractpText(url):
http = urllib3.PoolManager()
req = http.request('GET', url)
respData = RemoveThrashText(str(req.data))
regex = '<p>(.*?)</p>'
paragraphs = re.findall(regex, respData)
return paragraph
title('Extracting text from the <p> tag given a URL')
openBody()
writeWA("\n"ExtractpText("http://example.com/"))
autoPrettify()
# OR you could enclose it in a div/p/section/anything and style it, if you like
title('Extracting text from the <p> tag given a URL')
# Font
addFont("https://fonts.googleapis.com/css2?family=Roboto&display=swap")
openBody(background_color="#161a21")
p(writeWA("\n"ExtractpText("http://example.com/")), attr="class='p_class'")
# CSS
with cTags('.p_class') as p_class:
p_class.css(color="#dfe3eb", font_family="'Roboto', sans-serif")
autoPrettify()
''')
p(f'''Simple as that! You just use <code>writeWA()</code> for getting the job done!
{br} Or you could just do <code>p(ExtractpText("http://example.com/"))</code>, both work''')
p(f'''Similarly, variables and conditional statements can be added too''')
with open_tag('pre'):
writeWA(r'''
some_variable = 'doggo'
p(doggo)
# or use f-strings
p(f'This is my {some_variable}!')
# or use writeWA (even with f-strings, if the use case suits you)
if some_variable == 'dog':
writeWA(f'Grrr! This is NOT my {some_variable}')
else:
writeWA(f'Grrr! This is MY {some_variable}! I'd like you to come just a little closer.')
''')
autoPrettify()
# import webbrowser as wb
# wb.open('index.html')