Talking to Python from JavaScript (and Back Again!)

Talking to Python from JavaScript (and Back Again!)

Something a lot of beginners struggle with is the concept of passing data between different programming languages.

It's far more simple to understand that a value exists in a variable which can be passed around from function to function. However, to go beyond the program's edges we must serialized our data in some way. We'll look at two ways that these two languages can communicate. AJAX requests via the new Fetch API, and piping between local processes.

Serializing data means taking a value, object, or data structure, and translating it into a format that can be stored or transmitted. Most importantly, it needs to be put back together at the other end. Let’s take look at JavaScript Object Notation (JSON). JSON is a human-readable format and is straightforward for machines to read and write. The specification is small enough to read during one cup of coffee. Both JavaScript and Python have standard library methods to parse and write JSON.

JSON Crash Course

JSON is built on two data structures. Objects — key/value pairs like a JavaScript Object, and a Python Object or DictionaryArrays — a series of data like a JavaScript Array, and a Python List.

/* JavaScript
   Try this out in your developer console! */

var person = {"name":"Andrew", "loves":"Open Source"};
var asJSON = JSON.stringify(person);

// person is of type 'object'
console.log(person is of type ${typeof person});

// asJSON is of type 'string'
console.log(asJSON is of type ${typeof asJSON});

// We can convert it back to an object by parsing it
// asObject is of type 'object'
var asObject = JSON.parse(asJSON);
console.log(asObject is of type ${typeof asObject});

Let’s do the same in Python by using the standard library module json.

# python

animal = {'type':'cat', 'age':12}
as_json = json.dumps(animal)

print(type(animal)) # prints '<class 'dict'>'
print(type(as_json)) # prints '<class 'str'>'

now back again

as_object = json.loads(as_json)
print(type(as_object)) # prints '<class 'dict'>'

Recap: in JavaScript, you serialize to JSON with JSON.stringify() and parse with JSON.parse(). This works in the browser as well as Node.js. In Python, first import the json module then serialize with json.dumps() and parse with json.loads().

Talking via AJAX

In the past, this would be done with XMLHttpRequest but the relatively new Fetch API is far more pleasant to use. First, we’ll code up a small Python web server, and then we’ll look at passing JSON back and forth with the browser.

Flask is a ‘microframework’. It’s not only extremely fun to use, it’s also great to prototype with. We’ll use its jsonifymodule which writes/parses JSON as well as setting the correct response headers (an application/json mime type). It requires two OS-specific commands to install and run a debug server. For myself on OS X, they were pip install flask and FLASK_APP=hello.py flask run.

Code along or fork this repository to grab the code for all the examples.

# app.py
from flask import Flask, jsonify, request, render_template
app = Flask(name)

@app.route('/hello', methods=['GET', 'POST'])
def hello():

# POST request
if request.method == 'POST':
    print('Incoming..')
    print(request.get_json())  # parse as JSON
    return 'OK', 200

# GET request
else:
    message = {'greeting':'Hello from Flask!'}
    return jsonify(message)  # serialize and use JSON headers

@app.route('/test')
def test_page():
# look inside templates and serve index.html
return render_template('index.html')

With our server running and serving us a page we can run JavaScript on, let’s talk in JSON! We’ll send a GET request with the Fetch API and receive a greeting from Flask. Before writing a single line of client-side code, I always use Postman to test my web servers — it’s free and one of the industry standard tools for API testing and development.

I’m running the following snippets in <script> tags inside templates/index.html. There’s nothing else inside index.html so the rendered page is blank. Everything happens in the console.

// GET is the default method, so we don't need to set it
fetch('/hello')
.then(function (response) {
return response.text();
}).then(function (text) {
console.log('GET response text:');
console.log(text); // Print the greeting as text
});

// Send the same request
fetch('/hello')
.then(function (response) {
return response.json(); // But parse it as JSON this time
})
.then(function (json) {
console.log('GET response as JSON:');
console.log(json); // Here’s our JSON object
})

Awesome! We’ve got Python talking to client-side JavaScript using JSON for data serialization. Let’s flip it and send JSON to Python from the browser. We’ll use the Fetch API again but it will be a POST request instead of GET.

Beginner tip: Remember the difference between POST and GET. When you POST mail, you head to the post office with your letter filled with information. When you GET mail, you head to the post office again but this time you’re picking up something that’s been left for you.

// POST
fetch('/hello', {

// Specify the method
method: 'POST',

// A JSON payload
body: JSON.stringify({
    "greeting": "Hello from the browser!"
})

}).then(function (response) { // At this point, Flask has printed our JSON
return response.text();
}).then(function (text) {

console.log('POST response: ');

// Should be 'OK' if everything was successful
console.log(text);

});

With these two core building blocks, we’ve conquered JSON communication via HTTP. However, do note that you should be adding catch to the end of these Promises. I’ve only trimmed them for clarity. It’s better to handle errors gracefully so we can tell the user that they are disconnected or that there’s an error on our end. Docs for catch here.

If you’re talking to a Node.js web server with Python, you will probably reach for the requests module, which has syntax almost identical to the Fetch API.

Talking via processes

We’re going to spawn processes (both ways) so we can see what communication between Node.js and Python looks like. We’ll listen to the stdout stream of the child process in both instances. Let’s imagine that we have a program that reports data at irregular intervals. A temperature sensor. We want to listen to that program and store the values it reports.

Here’s our fake sensor program in Python. It prints data to stdout. We’ll catch it in Node.js.

# sensor.py

import random, time
while True:
time.sleep(random.random() * 5) # wait 0 to 5 seconds
temperature = (random.random() * 20) - 5 # -5 to 15
print(temperature, flush=True, end='')

When piping information in this way, it’s important to flush the stream so it reaches stdout when you expect it to (why do we fflush() in C?) . More information on Python flushing here. We also make sure that the end of the printed statement contains no extra information (even though parseFloat() would clean it!) by default it would be the newline character \n.

So, we’re Node.js and we want the current temperature as it’s reported. Let’s spawn sensor.py as a process and listen for the stdout event. Piping the data between the two running languages.

// temperature-listener.js

const { spawn } = require('child_process');
const temperatures = []; // Store readings

const sensor = spawn('python', ['sensor.py']);
sensor.stdout.on('data', function(data) {

// Coerce Buffer object to Float
temperatures.push(parseFloat(data));

// Log to debug
console.log(temperatures);

});

Flip it and reverse it

Now, let’s flip those roles. A Node.js sensor and a Python listener! This time we’ll try a different method, using a newline character (\n) to delimit the different readings instead of waiting for an event. We’ll add the data to a buffer until we hit a newline char. Once we do, we’ve collected a full reading and we can store it.

First, the equivalent sensor in Node.js.

// sensor.js

function reportReading() {
const temperature = (Math.random() * 20) - 5; // Range of -5 to 15
process.stdout.write(temperature + '\n'); // Write with newline char
setTimeout(reportReading, Math.random() * 5000); // Wait 0 to 5 seconds
}
reportReading();

Now in Python, a temperature listener program that will spawn the above code as a process.

# temperature-listener.py

import sys
from subprocess import Popen, PIPE

temperatures = [] # store temperatures
sensor = Popen(['node', 'sensor.js'], stdout=PIPE)
buffer = b''
while True:

# read sensor data one char at a time
out = sensor.stdout.read(1)

# after a full reading..
if out == b'\n':
    temperatures.append(float(buffer))
    print(temperatures)
    buffer = b''
else:
    buffer += out  # append to buffer

You can run node temperature-listener.js or python temperature-listener.py and the result will be the same. The array of temperatures will grow as new data arrives, and our debug log line will result in the following output.

[ 3.8075910850643098 ]
[ 3.8075910850643098, -1.5015912681923482 ]
[ 3.8075910850643098, -1.5015912681923482, 11.97817663641078 ]

We’ve seen two different ways of communicating between Python and JavaScript but if either of these aren’t for you — don’t fret! There are many ways to pass data between these two languages. Not limited to: named pipes, TCP sockets, WebSockets, and file polling.


Originally published by Andrew Healey at dev.to

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Machine Learning with Python, Jupyter, KSQL and TensorFlow

☞ Introduction to Python Microservices with Nameko

☞ Comparing Python and SQL for Building Data Pipelines

☞ Python Tutorial - Complete Programming Tutorial for Beginners (2019)


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

JavaScript vs Python: Will Python Replace JavaScript popularity by 2020?

JavaScript vs Python: Will Python Replace JavaScript popularity by 2020?

JavaScript is currently the most commonly used programming language but now Python is dishing out some stiff competition. Python has been steadily increasing in popularity so much so that it is now the fastest-growing programming language. So will Python Replace JavaScript popularity by 2020?

This is the Clash of the Titans!!

And no…I am not talking about the Hollywood movie (don’t bother watching it…it’s horrible!). I am talking about JavaScript and Python, two of the most popular programming languages in existence today.

JavaScript is currently the most commonly used programming language (and has been for quite some time!) but now Python is dishing out some stiff competition. Python has been steadily increasing in popularity so much so that it is now the fastest-growing programming language. So now the question is…Will Python Replace JavaScript popularity by 2020?

To understand the above question correctly, it is important to know more about JavaScript and Python as well as the reasons for their popularity. So let’s start with JavaScript first!

Why is JavaScript so popular?

JavaScript is a high-level, interpreted programming language that is most popular as a scripting language for Web pages. This means that if a web page is not just sitting there and displaying static information, then JavaScript is probably behind that. And that’s not all, there are even advanced versions of the language such as Node.js which is used for server-side scripting.

JavaScript is an extremely popular language. And if my word doesn’t convince you, here are the facts!!!

According to StackOverflow Developer Survey Results 2019, JavaScript is the most commonly used programming language, used by 69.7 % of professional developers. And this is a title it has claimed the past seven years in a row.

In addition to that, the most commonly used Web Frameworks are jQuery, Angular.js and React.js (All of which incidentally use JavaScript). Now if that doesn’t demonstrate JavaScript’s popularity, what does?!

Image Source: Stackoverflow

So now the question arises…Why is JavaScript so popular?

Well, some of the reasons for that are:

  • JavaScript is used both on the client-side and the server-side. This means that it runs practically everywhere from browsers to powerful servers. This gives it an edge over other languages that are not so versatile.
  • JavaScript implements multiple paradigms ranging from OOP to procedural. This allows developers the freedom to experiment as they want.
  • JavaScript has a large community of enthusiasts that actively back the language. Without this, it would have been tough for JavaScript to establish the number one position it has.
Can Python Replace JavaScript in Popularity?

Python is an interpreted, general-purpose programming language that has multiple uses ranging from web applications to data analysis. This means that Python can be seen in complex websites such as YouTube or Instagram, in cloud computing projects such as OpenStack, in Machine Learning, etc. (basically everywhere!)

Python has been steadily increasing in popularity so much so that it is the fastest-growing major programming language today according to StackOverflow Developer Survey Results 2019.

This is further demonstrated by this Google Trends chart showing the growth of Python as compared to JavaScript over the last 5 years:

As shown in the above data, Python recorded increased search interest as compared to JavaScript for the first time around November 2017 and it has maintained its lead ever since. This shows remarkable growth in Python as compared to 5 years ago.

In fact, Stack Overflow created a model to forecast its future traffic based on a model called STL and guess what…the prediction is that Python could potentially stay in the lead against JavaScript till 2020 at the least.

Image Source : Stackoverflow

All these trends indicate that Python is extremely popular and getting even more popular with time. Some of the reasons for this incredible performance of Python are given as follows:

  • Python is Easy To Use
    No one likes excessively complicated things and that’s one of the reasons for the growing popularity of Python. It is simple with an easily readable syntax and that makes it well loved by both seasoned developers and experimental students. In addition to this, Python is also supremely efficient. It allows developers to complete more work using fewer lines of code. With all these advantages, what’s not to love?!!
  • Python has a Supportive Community
    Python has been around since 1990 and that is ample time to create a supportive community. Because of this support, Python learners can easily improve their knowledge, which only leads to increasing popularity. And that’s not all! There are many resources available online to promote Python, ranging from official documentation to YouTube tutorials that are a big help for learners.
  • Python has multiple Libraries and Frameworks
    Python is already quite popular and consequently, it has hundreds of different libraries and frameworks that can be used by developers. These libraries and frameworks are really useful in saving time which in turn makes Python even more popular. Some of the popular libraries of Python are NumPy and SciPy for scientific computing, Django for web development, BeautifulSoup for XML and HTML parsing, scikit-learn for machine learning applications, nltk for natural language processing, etc.
So What’s the Conclusion?

While JavaScript is currently the most popular programming language, Python could soon outstrip it of this title based on its incredible growth rate. So it is entirely possible that Python could be the most popular programming language by 2020.

However, this will merely impact the relative popularity of these two languages and not specify which among them is the better language. That choice is entirely subjective and may depend on multiple factors such as project requirements, scalability, ease of learning as well as the future growth prospects.

JavaScript vs Python : Can Python outperform JavaScript in the next five years?

JavaScript vs Python : Can Python outperform JavaScript in the next five years?

JavaScript and Python are two influential programming languages for building a wide range of applications. While JavaScript has been the dominant programming language for many years, Python’s fast-growth threatens to dethrone the widely popular technology.

JavaScript and Python are two influential programming languages for building a wide range of applications. While JavaScript has been the dominant programming language for many years, Python’s fast-growth threatens to dethrone the widely popular technology.

This is the Clash of the Titans!!

And no…I am not talking about the Hollywood movie (don’t bother watching it…it’s horrible!). I am talking about JavaScript** and **Python, two of the most popular programming languages in existence today.

JavaScript is currently the most commonly used programming language (and has been for quite some time!) but now Python is dishing out some stiff competition. Python has been steadily increasing in popularity so much so that it is now the fastest-growing programming language. So now the question is…Will Python Replace JavaScript popularity by 2020?

To understand the above question correctly, it is important to know more about JavaScript and Python as well as the reasons for their popularity. So let’s start with JavaScript first!

Why is JavaScript so popular?

JavaScript is a high-level, interpreted programming language that is most popular as a scripting language for Web pages. This means that if a web page is not just sitting there and displaying static information, then JavaScript is probably behind that. And that’s not all, there are even advanced versions of the language such as Node.js which is used for server-side scripting.

JavaScript is an extremely popular language. And if my word doesn’t convince you, here are the facts!!!

According to StackOverflow Developer Survey Results 2019, JavaScript is the most commonly used programming language, used by 69.7 % of professional developers. And this is a title it has claimed the past seven years in a row.

In addition to that, the most commonly used Web Frameworks are jQuery, Angular.js and React.js (All of which incidentally use JavaScript). Now if that doesn’t demonstrate JavaScript’s popularity, what does?!

Image Source: Stackoverflow

So now the question arises…Why is JavaScript so popular?

Well, some of the reasons for that are:
JavaScript is used both on the client-side and the server-side. This means that it runs practically everywhere from browsers to powerful servers. This gives it an edge over other languages that are not so versatile.JavaScript implements multiple paradigms ranging from OOP to procedural. This allows developers the freedom to experiment as they want.JavaScript has a large community of enthusiasts that actively back the language. Without this, it would have been tough for JavaScript to establish the number one position it has.

Can Python Replace JavaScript in Popularity?

Python is an interpreted, general-purpose programming language that has multiple uses ranging from web applications to data analysis. This means that Python can be seen in complex websites such as YouTube or Instagram, in cloud computing projects such as OpenStack, in Machine Learning, etc. (basically everywhere!)

Python has been steadily increasing in popularity so much so that it is the fastest-growing major programming language today according to StackOverflow Developer Survey Results 2019.

This is further demonstrated by this Google Trends chart showing the growth of Python as compared to JavaScript over the last 5 years:

As shown in the above data, Python recorded increased search interest as compared to JavaScript for the first time around November 2017 and it has maintained its lead ever since. This shows remarkable growth in Python as compared to 5 years ago.

In fact, Stack Overflow created a model to forecast its future traffic based on a model called STL and guess what…the prediction is that Python could potentially stay in the lead against JavaScript till 2020 at the least.

Image Source : Stackoverflow

All these trends indicate that Python is extremely popular and getting even more popular with time. Some of the reasons for this incredible performance of Python are given as follows:
Python is Easy To UseNo one likes excessively complicated things and that’s one of the reasons for the growing popularity of Python. It is simple with an easily readable syntax and that makes it well loved by both seasoned developers and experimental students. In addition to this, Python is also supremely efficient. It allows developers to complete more work using fewer lines of code. With all these advantages, what’s not to love?!!Python has a Supportive CommunityPython has been around since 1990 and that is ample time to create a supportive community. Because of this support, Python learners can easily improve their knowledge, which only leads to increasing popularity. And that’s not all! There are many resources available online to promote Python, ranging from official documentation to YouTube tutorials that are a big help for learners.Python has multiple Libraries and FrameworksPython is already quite popular and consequently, it has hundreds of different libraries and frameworks that can be used by developers. These libraries and frameworks are really useful in saving time which in turn makes Python even more popular. Some of the popular libraries of Python are NumPy and SciPy for scientific computing, Django for web development, BeautifulSoup for XML and HTML parsing, scikit-learn for machine learning applications, nltk for natural language processing, etc.## So What’s the Conclusion?

While JavaScript is currently the most popular programming language, Python could soon outstrip it of this title based on its incredible growth rate. So it is entirely possible that Python could be the most popular programming language by 2020.

However, this will merely impact the relative popularity of these two languages and not specify which among them is the better language. That choice is entirely subjective and may depend on multiple factors such as project requirements, scalability, ease of learning as well as the future growth prospects.

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.