In PyScript, as well as JavaScript, there are only a few methods of getting data into your program:

  • Read data from the local file system
  • Read data from the network
  • Input data from the user
  • Create data inside your application

These methods appear simple to implement. However, when you go deep into browser application design, each topic expands greatly when you consider PWAs (Progressive Web Apps), caching fetch requests (Cache Storage), network offline access, application installation to the desktop, and more. These are the areas same that trip up HTML and JavaScript developers. If your goal is to become a competent Python in the Browser application developer, you must learn a lot more than just PyScript features.

My articles are written for the Python developer with little or no browser application development experience. The same techniques and limitations apply to JavaScript applications. In my opinion, people do not hate the JavaScript language. They actually hate the framework and interfaces that the browser provides.

In addition to my other articles, ten so far, I am planning several more articles that cover in detail each area that I mention in this article.

Read data from the local file system

Browser applications are not permitted to directly access the user’s local file system. All requests to read or write data require making an API request to the browser. The browser requires the user to have performed a gesture such as clicking a button. There are two primary APIs: FileReader and File System Access API. I wrote a multipart series on accessing the local file system using these APIs: Part 1 and Part 2.

Read data from the network

Browser applications cannot open network sockets. All network requests are performed by the Fetch API. This means all network requests must be one of the HTTP GET, POST, PUT, etc styles of requests. This is the norm for REST APIs. You cannot open network connections to services such as databases. Those services must provide a REST API to handle browser requests.

PyScript does offer a wrapper PyFetch but I find that implementation not useable for REST API because the HTTP response headers are not returned which is very important. I am planning an article on how to use fetch() to make HTTP requests.

Input data from the user

PyScript applications cannot request the user to type characters in response to Python input type of functions. PyScript applications must use HTML DOM elements to create a GUI that data is entered into. Then the application can read the data from those elements. See this link: User input and controls

Create data inside your application

Once your PyScript application is loaded and running, your application can create data and then store it using several methods:

  • POST the data back to the server using the Fetch API
  • Save the data on the local file system in response to the user making a gesture such as clicking a button.
  • Save data to the browser’s virtual file system – this is not persistent and data is lost on page refresh
  • Save data to the browser’s Storage, such as Local Storage and IndexedDB. See this link: Web Storage API.

More Information

Photography Credit

I write free articles about technology. Recently, I learned about which provides free images. The image in this article is courtesy of Wang Teck Heng at Pexels.