-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpy_out.html
119 lines (103 loc) · 5.92 KB
/
py_out.html
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
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<head>
<meta name="description" content="Sierra - A Python micro templating engine for web frameworks">
<meta name="keywords" content="html, css, js, jinja, django, python, web framework, templating">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documentation | Sierra - 2.0.0</title>
<link rel="apple-touch-icon" href="./img/sierra.png">
<link href="./css/style.css" rel="stylesheet">
<link href="./css/prism.css" rel="stylesheet">
<script src="./js/prism.js"></script>
</head>
<body>
<div class="container">
<h2>Working with Python Loops, Functions, Variables and Conditions</h2>
Displaying Python outputs on your web applications are actually pretty simple with Sierra.
To work with this, you can use <code class="language-py">writeWA()</code> or <code class="language-py">p()</code>, which has been covered on the main page of the documentation.
You can print variables, output functions, use loops, conditions or anything. <br>
Say you've loaded in a <code class="language-markup">.csv</code> file with pandas and write some quick code for displaying each row as a list:
<pre class="line-numbers"><code class="language-py">import pandas as pd
from sierra import *
df = pd.read_csv(r'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']</code></pre>
</div>
<div class="img-map">
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" rel="noopener noreferrer">image map</a>.
You can use f-strings and for loop to make it easy:
<pre class="line-numbers"><code class="language-py">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()</code></pre>
Here, you first display the image and give it an attribute <code class="language-py">usemap</code>, then you enter in four lists that contain the attributes of the three areas to be covered, open the <code class="language-html"><map></code> tag and map it to the attribute <code class="language-py">usemap</code> given to the image.<br>
Then you do a simple for loop and unpack each item in the lists with <code class="language-py">zip()</code>, open the <code class="language-html"><area></code> tag and simply use f-strings to enter in four different attributes to three different <code class="language-html"><area></code> tags which all come under <code class="language-html"><map></code>! <br>
So instead of manually entering in every single tag and attribute, you've used Python's list and for loop to get the job done.
</div>
<hr>
<div class="funcs">
With Sierra, using and displaying outputs of Python functions on your web application is made easy as cake
Here's an example of scraping a webpage with requests, given it's URL, and displaying all text within the <code class="language-html"><p></code> tag:
<pre class="line-numbers"><code class="language-py">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()</code></pre>
Simple as that! You just use <code class="language-py">writeWA()</code> for getting the job done!<br>
Or you could just do <code class="language-py">p(ExtractpText("http://example.com/"))</code>, both work.
Similarly, variables and conditional statements can be added too.
<pre class="line-numbers"><code class="language-py">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.')</code></pre>
</div>
</body>
</html>