hari3485 commited on
Commit
bb0ed14
·
verified ·
1 Parent(s): 1749ad2

Update pages/hari.py

Browse files
Files changed (1) hide show
  1. pages/hari.py +97 -8
pages/hari.py CHANGED
@@ -196,15 +196,104 @@ for i, chunk in enumerate(chunks):
196
 
197
  # Semi-Structured Data - JSON Page
198
  def json_details_page():
199
- st.title("Semi-Structured Data - JSON Details")
200
- st.markdown("""
201
- **JSON** (JavaScript Object Notation) is a lightweight data-interchange format.
202
 
203
- - Represents data as key-value pairs.
204
- - Widely used for APIs and configuration files.
205
- - Readable and writable using Python's `json` module.
206
- """)
207
- st.code('import json\ndata = json.load(open("file.json"))', language="python")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  if st.button("Back to Home"):
210
  st.session_state['page'] = "home"
 
196
 
197
  # Semi-Structured Data - JSON Page
198
  def json_details_page():
 
 
 
199
 
200
+ import pandas as pd
201
+ import requests
202
+
203
+ # Page configuration
204
+ st.set_page_config(page_title="JSON & API Tutorial", layout="wide")
205
+
206
+ # Define colors
207
+ main_heading_color = "blue"
208
+ sub_heading_color = "green"
209
+ bullet_point_color = "black"
210
+
211
+ # Main Title
212
+ st.markdown(f"<h1 style='color:{main_heading_color};'>JSON and API Tutorial</h1>", unsafe_allow_html=True)
213
+
214
+ # Section 1: Handling JSON Files
215
+ st.markdown(f"<h2 style='color:{sub_heading_color};'>Handling JSON Files</h2>", unsafe_allow_html=True)
216
+ st.markdown(f"<h3 style='color:{sub_heading_color};'>Introduction</h3>", unsafe_allow_html=True)
217
+ st.markdown(
218
+ f"<ul style='color:{bullet_point_color};'>"
219
+ f"<li>JSON (JavaScript Object Notation) is the second most commonly used data format after CSV.</li>"
220
+ f"<li>It is widely used, especially in APIs.</li>"
221
+ f"<li>JSON data can be either structured or semi-structured.</li>"
222
+ f"</ul>",
223
+ unsafe_allow_html=True,
224
+ )
225
+
226
+ st.markdown(f"<h3 style='color:{sub_heading_color};'>Default JSON Format</h3>", unsafe_allow_html=True)
227
+ st.code('{"Name": ["P1", "P2"], "Age": [23, 24]}', language="json")
228
+
229
+ # Code example for reading JSON
230
+ st.markdown(f"<h3 style='color:{sub_heading_color};'>Reading JSON Files in Python</h3>", unsafe_allow_html=True)
231
+ st.code(
232
+ """
233
+ import pandas as pd
234
+ data = '{"Name": ["P1", "P2"], "Age": [23, 24]}'
235
+ df = pd.read_json(data)
236
+ print(df)
237
+ """,
238
+ language="python",
239
+ )
240
+
241
+ # Section 2: JSON Formats in Pandas
242
+ st.markdown(f"<h2 style='color:{sub_heading_color};'>JSON Formats in Pandas</h2>", unsafe_allow_html=True)
243
+ st.markdown(
244
+ f"<ul style='color:{bullet_point_color};'>"
245
+ f"<li><b>Orient = 'index':</b> Indices become main keys and column names become subkeys.</li>"
246
+ f"<li><b>Orient = 'columns':</b> Column names become main keys and indices become subkeys.</li>"
247
+ f"<li><b>Orient = 'values':</b> JSON is converted as a list of values.</li>"
248
+ f"<li><b>Orient = 'split':</b> Stores data along with columns and indices.</li>"
249
+ f"</ul>",
250
+ unsafe_allow_html=True,
251
+ )
252
+
253
+ # Section 3: Collecting Data from APIs
254
+ st.markdown(f"<h2 style='color:{sub_heading_color};'>Collecting Data from APIs</h2>", unsafe_allow_html=True)
255
+ st.markdown(
256
+ f"<ul style='color:{bullet_point_color};'>"
257
+ f"<li>API (Application Programming Interface) is a bridge that enables communication between two applications.</li>"
258
+ f"<li>It uses HTTP protocols to exchange data securely.</li>"
259
+ f"<li>If the response code is <b>200</b>, the request was successful.</li>"
260
+ f"<li>For accessing secure data, you may need an API key.</li>"
261
+ f"</ul>",
262
+ unsafe_allow_html=True,
263
+ )
264
+
265
+ # Code example for using an API
266
+ st.markdown(f"<h3 style='color:{sub_heading_color};'>Example: Fetching Data from an API</h3>", unsafe_allow_html=True)
267
+ st.code(
268
+ """
269
+ import requests
270
+ import pandas as pd
271
+
272
+ url = "https://api.example.com/data"
273
+ response = requests.get(url)
274
+
275
+ if response.status_code == 200:
276
+ data = response.json()
277
+ df = pd.json_normalize(data)
278
+ print(df)
279
+ else:
280
+ print(f"Failed to fetch data. Status code: {response.status_code}")
281
+ """,
282
+ language="python",
283
+ )
284
+
285
+ # Google Colab Link
286
+ st.markdown(f"<h2 style='color:{main_heading_color};'>Try It on Google Colab</h2>", unsafe_allow_html=True)
287
+ colab_link = "https://colab.research.google.com/"
288
+ st.markdown(f'<a href="{https://colab.research.google.com/drive/1pIg_zmj04lVmPTdiTU2bU9BLAR2mS5wi?usp=sharing}" target="_blank"><button style="background-color:{main_heading_color}; color:white;">Open Google Colab</button></a>', unsafe_allow_html=True)
289
+
290
+ # Back Button
291
+ if st.button("Back"):
292
+ st.experimental_rerun()
293
+
294
+ # Footer
295
+ st.markdown("---")
296
+ st.markdown(f"<h3 style='color:{sub_heading_color};'>Happy Learning!</h3>", unsafe_allow_html=True)
297
 
298
  if st.button("Back to Home"):
299
  st.session_state['page'] = "home"