My Little coding practice

 HTML_FORM = '''

<!doctype html>
<html>
<head>
<title>Submit Dictionary</title>
</head>
<body>
<h2>Enter a Dictionary</h2>
<form method="post" action="/display">
<textarea name="data" rows="5" cols="50" placeholder='{"key1": "value1", "key2": "value2"}'></textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
'''

HTML_TABLE = '''
<!doctype html>
<html>
<head>
<title>Dictionary Table</title>
</head>
<body>
<h2>Dictionary Data</h2>
<table border="1">
<tr><th>Key</th><th>Value</th></tr>
{% for key, value in data.items() %}
<tr><td>{{ key }}</td><td>{{ value }}</td></tr>
{% endfor %}
</table>
<br>
<a href="/">Back</a>
</body>
</html>
'''


@app.route('/', methods=['GET'])
def home():
return HTML_FORM


@app.route('/display', methods=['POST'])
def display():
try:
data = request.form['data']
data_dict = eval(data) # Unsafe for real-world use, replace with json.loads()
if not isinstance(data_dict, dict):
raise ValueError("Input is not a dictionary")
except Exception as e:
return f"Invalid input: {e} <br><a href='/'>Back</a>"
return render_template_string(HTML_TABLE, data=data_dict)



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>List of Dict Table</title>
</head>
<body>
    <h2>Data Table</h2>
    <form action="/send" method="post">
        <table border="1">
            <thead>
                <tr>
                    <th>Select</th>
                    <th>Name</th>
                    <th>Age</th>
                    <th>City</th>
                </tr>
            </thead>
            <tbody>
                {% for row in data %}
                <tr>
                    <td>
                        <input type="radio" name="row_id" value="{{ row.id }}" required>
                    </td>
                    <td>{{ row.Name }}</td>
                    <td>{{ row.Age }}</td>
                    <td>{{ row.City }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        <br>
        <button type="submit">Send</button>
    </form>
</body>
</html>

Comments

Popular Posts