2

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

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
Hype Adal
  • 21
  • 3
  • 1
    I wrote two articles about files and file systems in PyScript: https://www.jhanley.com/pyscript-files-and-file-systems-part-1/ and https://www.jhanley.com/pyscript-files-and-file-systems-part-2/ – John Hanley Jul 02 '22 at 17:47
  • sorry but your code example from part 2 for writing to a local file throws an exception: ShowSaveFilePicker. I couldn't quite get what the error indicates here is a ss if u wanna see for the console [link](https://cdn.discordapp.com/attachments/992668561162125322/993585479058464868/unknown.png) – Hype Adal Jul 04 '22 at 18:32
  • I would need details on the code and the error. I use that code in dozens of test applications. – John Hanley Jul 04 '22 at 18:33
  • [link]https://pastebin.com/XFuqMbVQ for the `` it is the exact code that u provided just wanted to check if it works also the [link]https://cdn.discordapp.com/attachments/993308988072529981/993587302112698448/unknown.png for file tree – Hype Adal Jul 04 '22 at 18:38
  • Which browser are you using? PyScript is being tested and developed mostly on Chrome but your problem is not a PyScript issue. The ShowSaveFilePicker API is not yet available in Firebox, IE, Safari, etc as I mentioned in the article. https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker – John Hanley Jul 04 '22 at 18:47
  • Use the older APIs as mentioned in Part 1. – John Hanley Jul 04 '22 at 18:47
  • I'm using brave browser – Hype Adal Jul 04 '22 at 18:50
  • but the older API from part 1 only lets you access and read but does it lets you write? – Hype Adal Jul 04 '22 at 18:57
  • The older API supports read and write of files. – John Hanley Jul 04 '22 at 19:00
  • but can I do it dynamically without having to click any button just on button click save it automatically inside the JSON file which is situated in the local disk? – Hype Adal Jul 04 '22 at 19:07
  • 1
    The browser requires that the user make a gesture such as clicking a button. You cannot access the file system directly, only thru the browser interfaces. That is not a PyScript limitation, that is a browser security limitation. – John Hanley Jul 04 '22 at 19:08
  • soo... Do I need to use a rest API for that? – Hype Adal Jul 04 '22 at 19:10

0 Answers0