code
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<link rel = 'stylesheet' href = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
<link rel = 'stylesheet' href = "style.css">
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<h1>
User Authenticator
</h1>
<div id="login_field">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
<button type="submit" id="brofist" class="btn btn-primary" pys-onClick="danm">sign up</button>
<a href="index.html">already signed up? login here</a>
</div>
<style>
#login_field {
margin-top: 20px;
}
</style>
<py-env>
- paths:
- ./login.json
</py-env>
<py-script>
import json
def danm(*args, **kwargs):
username = document.getElementById("username").value
password = document.getElementById("password").value
print(username, password)
#take username and password and check if they are in the json file
#if they are,print user already exists
#if they aren't, add their username and password to the json file
#and print user created
with open("./login.json","r") as f:
users = json.load(f)
print(users)
if username in users:
print("user already exists")
else:
users[username] = password
print("user created")
with open("./login.json", "w") as f:
json.dump(users, f)
</py-script>
</body>
</html>
ERROR
no error
but when I try to save the username and password in the local JSON file it wouldn't wanna save tried out the <py-env></py-env>
it saves but not in the local JSON file, the data goes away when refreshed, please help me solve the problem thanks.
What I want
when the user types in the user and pass click the button which triggers a pyscript function to open a JSON file and save the data inside it with conditions like if the user already exists then don't save instead print out user exists