-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythonbasics.py
298 lines (244 loc) · 7.56 KB
/
pythonbasics.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
Datascience Cheat Sheet
There is also an accompanying png version
https://github.com/daniellewisDL/streamlit-cheat-sheet
v0.71.0 November 2020 Daniel Lewis and Austin Chen
"""
import streamlit as st
from pathlib import Path
import base64
# Initial page config
# st.set_page_config(
# page_title='Datascience cheat sheet',
# layout="wide",
# initial_sidebar_state="expanded",
# )
def main():
cs_sidebar()
cs_body()
return None
# Thanks to streamlitopedia for the following code snippet
def img_to_bytes(img_path):
img_bytes = Path(img_path).read_bytes()
encoded = base64.b64encode(img_bytes).decode()
return encoded
# sidebar
def cs_sidebar():
#
# st.sidebar.code('$ pip install streamlit')
#
# st.sidebar.markdown('Import convention')
# st.sidebar.code('>>> import streamlit as st')
#
# st.sidebar.markdown('__Add widgets to sidebar__')
# st.sidebar.code('''
# st.sidebar.<widget>
# >>> a = st.sidebar.radio(\'R:\',[1,2])
# ''')
#
# st.sidebar.markdown('__Command line__')
# st.sidebar.code('''
# $ streamlit --help
# $ streamlit run your_script.py
# $ streamlit hello
# $ streamlit config show
# $ streamlit cache clear
# $ streamlit docs
# $ streamlit --version
# ''')
#
# st.sidebar.markdown('__Pre-release features__')
# st.sidebar.markdown('[Beta and experimental features](https://docs.streamlit.io/en/0.70.0/api.html#beta-and-experimental-features)')
# st.sidebar.code('''
# pip uninstall streamlit
# pip install streamlit-nightly --upgrade
# ''')
# st.sidebar.markdown('''[<img src='data:image/png;base64,{}' class='img-fluid' width=32 height=32>](https://github.com/daniellewisDL/streamlit-cheat-sheet) <small>st.cheat_sheet v0.71.0 | Nov 2020</small>'''.format(img_to_bytes("brain.png")), unsafe_allow_html=True)
return None
##########################
# Main body of cheat sheet
##########################
#
def cs_body():
# Magic commands
st.markdown("""
<style>
body {
color: #00628B;
background-color: #d6ffe8;
}
</style>
""", unsafe_allow_html=True)
# st.title("Data Science Cheat App")
# type = ["Python Basics", "Pandas Basics", "Numpy Basics", "ML Algorithms"]
# st.sidebar.markdown('Choose the cheat sheet to learn')
# activity = st.sidebar.radio("Choose one from down", type)
# if "Python Basics" in activity:
st.subheader("Python Basics")
col1, col2, col3 = st.beta_columns(3)
col1.subheader('Variables and Data Types')
col1.code('''# Variable Assignment `st.write()`
\'\'\' _This_ is some __Markdown__ \'\'\'
>>> x = 10
>>> x
5
''')
# Display text
col1.subheader('Calculation With Variables')
col1.code('''
>> x+2 ------- Sum of two variable
7
>>> x-2 ------- Subtraction of two variables
3
>>> x*2 ------- Multiplication of two variables
10
>>> x**2 ------- Exponentiation of a Variable
25
>>> x%2 ------- Remainder of a Variable
1
>>> x/float(2) ------- Division of a variable
2.5
# * optional kwarg unsafe_allow_html = True
''')
# Display data
col1.subheader('Types and Type Conversion')
col1.code('''
str() '5', '3.45', 'True' ------- Variables to strings
int() 5, 3, 1 ------- Variables to integers
float() 5.0, 1.0 ------- Variables to floats
bool() True, True, True ------- Variables to booleans
''')
# Display charts
col1.subheader('String')
col1.code('''
>>> my_string = 'thisStringIsAwesome'
>>> my_string
'thisStringIsAwesome'
''')
# Display media
col1.write('String Operations')
col1.code('''
>>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
>>> my_string + 'Innit'
'thisStringIsAwesomeInnit'
>>> 'm' in my_string
True
''')
col1.write('String Methods')
col1.code('''
>>> my_string.upper() ------- String to uppercase
>>> my_string.lower() ------- String to lowercase
>>> my_string.count('w') ------- Count String elements
>>> my_string.replace('e', 'i') ------- Replace String elements
>>> my_string.strip() ------- Strip whitespaces
''')
# Display interactive widgets
col2.subheader('Lists')
col2.code('''
>> a = 'is'
>>> b = 'nice'
>>> my_list = ['my', 'list', a, b]
>>> my_list2 = [[4,5,6,7], [3,4,5,6]]
''')
col2.subheader('Selecting List Elements')
col2.write('Subset')
col2.code('''
>>> my_list[1] ------- Select item at index 1
>>> my_list[-3] ------- Select 3rd last item
''')
col2.write('Slice')
col2.code('''
>>> my_list[1:3] ------- Select items at index 1 and 2
>>> my_list[1:] ------- Select items after index 0
>>> my_list[:3] ------- Select items before index 3
>>> my_list[:] ------- Copy my_list
''')
col2.write('Subset List of Lists')
col2.code('''
>>> my_list2[1][0] ------- my_list[list][itemOfList]
>>> my_list2[1][:2]
''')
# Control flow
col2.subheader('List Operations')
col2.code('''
>>> my_list + my_list
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
True
''')
# Lay out your app
col2.subheader('List Methods')
col2.code('''
>>> my_list.index(a) ------- Get the index of an item
>>> my_list.count(a) ------- Count an item
>>> my_list.append('!') ------- Append an item at a time
>>> my_list.remove('!') ------- Remove an item
>>> del(my_list[0:1]) ------- Remove an item
>>> my_list.reverse() ------- Reverse the list
>>> my_list.extend('!') ------- Append an item
>>> my_list.pop(-1) ------- Remove an item
>>> my_list.insert(0,'!') ------- Insert an item
>>> my_list.sort() ------- Sort the list
''')
# Libraries
col3.subheader('Libraries')
col3.write('Import Libraries')
col3.code('''
>>> import numpy
>>> import numpy as np
''')
col3.write('Selective import')
col3.code('''
>>> from math import pi
''')
# Placeholders, help, and options
col3.subheader('Numpy Arrays')
col3.code('''
>>> my_list = [1, 2, 3, 4]
>>> my_array = np.array(my_list)
>>> my_2darray = np.array([[1,2,3],[4,5,6]])
''')
# Mutate data
col3.subheader('Selecting Numpy Array Elements')
col3.write('Subset')
col3.code('''
>>> my_array[1] 2 ------- Select item at index 1
''')
col3.write('Slice')
col3.code('''
>>> my_array[0:2] ------- Select items at index 0 and 1
array([1, 2])
''')
col3.write('Subsets 2D Numpy Arrays')
col3.code('''
>>> my_2darray[:,0] ------- my_2darray[rows, columns]
array([1, 4])
''')
col3.subheader('Numpy Array Operations')
col3.code('''
>>> my_array > 3
array([False, False, False, True], dtype=bool)
>>> my_array * 2
array([2, 4, 6, 8])
>>> my_array + np.array([5, 6, 7, 8])
array([6, 8, 10, 12])
''')
# Optimize performance
col3.subheader('Numpy Array Functions')
col3.code('''
>>> my_array.shape ------- Get the dimensions of the array
>>> np.append(other_array) ------- Append items to an array
>>> np.insert(my_array, 1, 5) ------- Insert items in an array
>>> np.delete(my_array,[1]) ------- Delete items in an array
>>> np.mean(my_array) ------- Mean of the array
>>> np.median(my_array) ------- Median of the array
>>> my_array.corrcoef() ------- Correlation coefficient
>>> np.std(my_array) ------- Standard deviation
''')
return None
# Run main()
if __name__ == '__main__':
main()