Loading Python Runtime...
Downloading Pyodide...

Day 4: APIs, JSON & Real-world Automation

📋 Today's Agenda

View Topics
Topic
Working with JSON and CSV
Using Python with REST APIs
System monitoring with psutil
☕ Break
Building Admin Dashboard
Final Q&A and Wrap-up

📊 JSON and CSV Data

Working with JSON

import json

# Create a JSON file first
config = {"host": "localhost", "port": 8080, "debug": True}
with open('config.json', 'w') as file:
    json.dump(config, file, indent=2)
print("config.json created!")
# Read JSON file
with open('config.json', 'r') as file:
    data = json.load(file)
    print(data)
# Write JSON file
config = {"host": "localhost", "port": 8080}
with open('config.json', 'w') as file:
    json.dump(config, file, indent=2)
# Parse JSON string
json_string = '{"name": "server01", "status": "online"}'
data = json.loads(json_string)
print(data['name'])
# Create JSON from dictionary
server_info = {
    "hostname": "web-server-01",
    "ip": "192.168.1.100",
    "services": ["http", "https", "ssh"]
}
json_data = json.dumps(server_info, indent=2)
print(json_data)
# Update JSON data
with open('config.json', 'r') as file:
    data = json.load(file)
data['port'] = 9000
with open('config.json', 'w') as file:
    json.dump(data, file, indent=2)

Working with CSV

import csv

# Create a CSV file first (with headers for DictReader)
users = [
    {'username': 'admin', 'role': 'administrator'},
    {'username': 'user1', 'role': 'user'},
    {'username': 'user2', 'role': 'user'}
]
with open('users.csv', 'w', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=['username', 'role'])
    writer.writeheader()
    writer.writerows(users)
print("users.csv created!")
# Read CSV file
with open('users.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
# Read CSV with headers
with open('users.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(f"User: {row['username']}, Role: {row['role']}")
# Write CSV file
users = [
    ['admin', 'administrator'],
    ['user1', 'user'],
    ['user2', 'user']
]
with open('new_users.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(users)
# Write CSV with headers
users = [
    {'username': 'admin', 'role': 'administrator'},
    {'username': 'user1', 'role': 'user'}
]
with open('users.csv', 'w', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=['username', 'role'])
    writer.writeheader()
    writer.writerows(users)
# Process CSV data
def count_users_by_role(filename):
    role_count = {}
    with open(filename, 'r') as file:
        reader = csv.DictReader(file)
        for row in reader:
            role = row['role']
            role_count[role] = role_count.get(role, 0) + 1
    return role_count

🌐 REST APIs

Note: The requests library needs special setup in Pyodide. These examples use urllib as a fallback for browser compatibility.
import json
try:
    import urllib.request
    
    # Simple GET request (using urllib)
    url = 'https://jsonplaceholder.typicode.com/todos/1'
    with urllib.request.urlopen(url) as response:
        data = json.loads(response.read().decode())
        print(f"Title: {data['title']}")
        print(f"Completed: {data['completed']}")
    
except Exception as e:
    print(f"Network request failed: {e}")
    print("This is expected in the browser environment.")

API Request Functions

import json
try:
    import urllib.request
    
    def api_get(url):
        try:
            with urllib.request.urlopen(url) as response:
                return json.loads(response.read().decode())
        except Exception as e:
            return {"error": str(e)}
    
    # Example usage
    data = api_get('https://jsonplaceholder.typicode.com/todos/1')
    print(data)
    
except ImportError:
    print("urllib not available")

📊 System Monitoring with psutil

Note: psutil is available in Pyodide but some functions may be limited. The browser environment provides simulated values.
import psutil

# Get CPU usage
try:
    cpu_percent = psutil.cpu_percent()
    print(f"CPU usage: {cpu_percent}%")
except:
    print("CPU monitoring not available in this environment")
# Get memory info
try:
    memory = psutil.virtual_memory()
    print(f"Memory usage: {memory.percent}%")
    print(f"Available: {memory.available // (1024**3)} GB")
except:
    print("Memory monitoring not available")
# Get disk usage
try:
    disk = psutil.disk_usage('/')
    print(f"Disk usage: {disk.percent}%")
    print(f"Free: {disk.free // (1024**3)} GB")
except:
    print("Disk monitoring not available")

Process Monitoring

import psutil

# List processes (limited in browser)
try:
    for proc in psutil.process_iter(['pid', 'name'])[:5]:
        print(f"PID: {proc.info['pid']}, Name: {proc.info['name']}")
except Exception as e:
    print(f"Process listing: {e}")
# Get process details (simulated)
try:
    proc = psutil.Process()
    print(f"CPU percent: {proc.cpu_percent()}")
    print(f"Memory percent: {proc.memory_percent()}")
except:
    print("Process details not available")

Collect System Data

import psutil
import platform

def collect_system_data():
    data = {
        'system': platform.system(),
        'hostname': platform.node(),
        'cpu_count': psutil.cpu_count(),
    }
    try:
        data['cpu_percent'] = psutil.cpu_percent()
        data['memory_percent'] = psutil.virtual_memory().percent
        data['disk_percent'] = psutil.disk_usage('/').percent
    except:
        pass
    return data
info = collect_system_data()
for key, value in info.items():
    print(f"{key}: {value}")

📈 Report Generation

import json
import datetime

def generate_report(data):
    report = f"""
System Report
============
CPU Usage: {data.get('cpu_percent', 'N/A')}%
Memory Usage: {data.get('memory_percent', 'N/A')}%
Disk Usage: {data.get('disk_percent', 'N/A')}%
Generated: {datetime.datetime.now()}
"""
    return report
def save_report(data):
    timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
    filename = f'system_report_{timestamp}.json'
    with open(filename, 'w') as file:
        json.dump(data, file, indent=2)
    print(f"Report saved to {filename}")
def generate_csv_report(processes, filename='process_report.csv'):
    try:
        import csv
        with open(filename, 'w', newline='') as file:
            if processes:
                writer = csv.DictWriter(file, fieldnames=processes[0].keys())
                writer.writeheader()
                writer.writerows(processes)
        print(f"CSV report saved to {filename}")
    except Exception as e:
        print(f"CSV generation: {e}")
# Generate HTML report
def generate_html_report(data):
    html = f"""
<html>
<head><title>System Report</title></head>
<body>
<h1>System Report</h1>
<p>CPU: {data.get('cpu_percent', 'N/A')}%</p>
<p>Memory: {data.get('memory_percent', 'N/A')}%</p>
<p>Disk: {data.get('disk_percent', 'N/A')}%</p>
</body>
</html>
"""
    with open('report.html', 'w') as file:
        file.write(html)
    print("HTML report saved to report.html")

🚨 Alert System

def check_alerts(data, thresholds=None):
    if thresholds is None:
        thresholds = {'cpu': 80, 'memory': 80, 'disk': 90}
    
    alerts = []
    if data.get('cpu_percent', 0) > thresholds.get('cpu', 80):
        alerts.append("High CPU usage")
    if data.get('memory_percent', 0) > thresholds.get('memory', 80):
        alerts.append("High memory usage")
    if data.get('disk_percent', 0) > thresholds.get('disk', 90):
        alerts.append("Low disk space")
    return alerts
def log_alert(alert):
    import datetime
    timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    with open('alerts.log', 'a') as file:
        file.write(f"[{timestamp}] {alert}\\n")
    print(f"Alert logged: {alert}")
def process_alerts(data):
    alerts = check_alerts(data)
    for alert in alerts:
        print(f"ALERT: {alert}")
        log_alert(alert)
    if not alerts:
        print("All systems normal")
# Test with sample data
sample_data = {'cpu_percent': 85, 'memory_percent': 75, 'disk_percent': 88}
process_alerts(sample_data)

🛠️ Hands-On: Admin Dashboard

import psutil
import datetime

def simple_dashboard():
    try:
        data = {
            'cpu_percent': psutil.cpu_percent(),
            'memory_percent': psutil.virtual_memory().percent,
            'disk_percent': psutil.disk_usage('/').percent
        }
    except:
        data = {'cpu_percent': 'N/A', 'memory_percent': 'N/A', 'disk_percent': 'N/A'}
    
    print("=== System Dashboard ===")
    print(f"CPU: {data['cpu_percent']}%")
    print(f"Memory: {data['memory_percent']}%")
    print(f"Disk: {data['disk_percent']}%")
    
    alerts = check_alerts(data)
    if alerts:
        print("\\n=== Alerts ===")
        for alert in alerts:
            print(f"WARNING: {alert}")
    else:
        print("\\nAll systems normal!")
def dashboard_with_reports(data):
    print("=== Current Status ===")
    print(f"CPU: {data.get('cpu_percent', 'N/A')}%")
    print(f"Memory: {data.get('memory_percent', 'N/A')}%")
    print(f"Disk: {data.get('disk_percent', 'N/A')}%")
    
    # Save report
    save_report(data)
    generate_html_report(data)

🌐 Real-Time Dashboards with Streamlit

Note: Streamlit requires local Python installation. Install with: pip install streamlit Run with: streamlit run dashboard.py
# Install: pip install streamlit psutil
# Save as: dashboard.py
# Run: streamlit run dashboard.py

import streamlit as st
import psutil

st.title('Admin System Dashboard')

cpu = psutil.cpu_percent()
memory = psutil.virtual_memory().percent
disk = psutil.disk_usage('/').percent

st.metric('CPU Usage', f'{cpu}%')
st.metric('Memory Usage', f'{memory}%')
st.metric('Disk Usage', f'{disk}%')

if cpu > 80:
    st.error('High CPU usage!')
if memory > 80:
    st.warning('High memory usage!')
if disk > 90:
    st.error('Low disk space!')

✅ What We Did Today

📚 Resources

🎓 Course Summary

🎉 Congratulations!

You've completed the Python for Junior Admins course!

You're now ready to automate and manage systems with Python.

Python by srk
Logo
💬 save me