Wednesday, February 24, 2021

Real Python: Brython: Python in Your Browser

If you’re a web developer who prefers writing Python over JavaScript, then Brython, a Python implementation that runs in the browser, may be an appealing option.

JavaScript is the de facto language of front-end web development. Sophisticated JavaScript engines are an inherent part of all modern Internet browsers and naturally drive developers to code front-end web applications in JavaScript. Brython offers the best of both worlds by making Python a first-class citizen language in the browser and by having access to all the existing JavaScript libraries and APIs available in the browser.

In this tutorial, you’ll learn how to:

  • Install Brython in your local environment
  • Use Python in a browser
  • Write Python code that interacts with JavaScript
  • Deploy Python with your web application
  • Create browser extensions with Python
  • Compare Brython with other Python implementations for web applications

As an intermediate Python developer familiar with web development, you’ll get the most out of this tutorial if you also have some knowledge of HTML and JavaScript. For a JavaScript refresher, check out Python vs JavaScript for Pythonistas.

You can download the source material for the examples in this tutorial by clicking the link below:

Get the Source Code: Click here to get the source code you’ll use to learn about using Brython to run Python in the browser in this tutorial.

Running Python in the Browser: The Benefits

Although JavaScript is the ubiquitous language of front-end web development, the following points may apply to you:

  • You may not like writing code in JavaScript.
  • You may want to leverage your Python skills.
  • You may not want to spend the time to learn JavaScript to explore browser technologies.
  • You may not like being forced to learn and use JavaScript to implement a web application.

Whatever the reason, many developers would prefer a Python-based alternative to JavaScript for leveraging the power of the browser.

There are several benefits of running Python in the browser. It allows you to:

  • Execute the same Python code in the server and the browser
  • Work with various browser APIs using Python
  • Manipulate the Document Object Model (DOM) with Python
  • Use Python to interact with existing JavaScript libraries like Vue.js and jQuery
  • Teach the Python language to Python students with the Brython editor
  • Keep the sense of fun while programming in Python

One side effect of using Python in the browser is a loss of performance compared to the same code in JavaScript. However, this drawback doesn’t outweigh any of the benefits outlined above.

Implementing Isomorphic Web Development

Isomorphic JavaScript, or Universal JavaScript, emphasizes that JavaScript applications should run on both the client and the server. This is assuming that the back end is JavaScript based, namely a Node server. Python developers using Flask or Django can also apply the principles of isomorphism to Python, provided that they can run Python in the browser.

Brython allows you to build the front end in Python and share modules between the client and the server. For example, you can share validation functions, like the following code that normalizes and validates US phone numbers:

 1import re
 2
 3def normalize_us_phone(phone: str) -> str:
 4    """Extract numbers and digits from a given phone number"""
 5    return re.sub(r"[^\da-zA-z]", "", phone)
 6
 7def is_valid_us_phone(phone: str) -> bool:
 8    """Validate 10-digit phone number"""
 9    normalized_number = normalize_us_phone(phone)
10    return re.match(r"^\d{10}$", normalized_number) is not None

normalize_us_phone() eliminates any nonalphanumeric characters, whereas is_valid_us_phone() returns True if the input string contains exactly ten digits and no alphabetic characters. The same code can be shared between processes running on a Python server and a client built with Brython.

Accessing Web APIs

Internet browsers expose standardized web APIs to JavaScript. These standards are part of the HTML Living Standard. Some web API examples include:

Brython allows you to both use the web APIs and interact with JavaScript. You’ll work with some of the web APIs in a later section.

Prototyping and JavaScript Libraries

Python is often used to prototype snippets of code, language constructs, or bigger ideas. With Brython, this common coding practice becomes available in your browser. For example, you can use the Brython console or the interactive editor to experiment with a snippet of code.

Read the full article at https://realpython.com/brython-python-in-browser/ »


[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...