4

Let's say I have a simple Python variable, such as the following:

x = 5

I'd like to create a web page that uses javascript to further process that variable, running on the same computer (a Raspberry Pi).

<!DOCTYPE html>
<html>
<body>

<script>
document.write(x);
</script>

</body>
</html>

Because the Raspberry Pi uses a micro SD card, I would like to limit the number of read/write cycles. Is there a way to store this Python variable in memory (or some other way) which could be accessed by the Javascript code?

I know some will suggest rewriting the Python code in Javascript, but I already have a large Python program that I would prefer NOT to have to rewrite due to time and the original effort involved in its creation. And if you are curious, I'm using the Javascript to display data with Highcharts - which is preferred for my project.

Alligator
  • 691
  • 3
  • 11
  • 21

3 Answers3

2

I can't think of a direct way of achieving this. But I think you could use socket-io on both sides and communicate through it.

EDIT:

As S.Pellegrino noted, depending on op's needs, building a REST API from what is called in the question a 'large Python program' might be a good option.

89f3a1c
  • 1,430
  • 1
  • 14
  • 24
  • 3
    And if it doesn't have to be real-time, you could just expose a REST API giving access to the internal state of your python program (directly from the inside of your python script) and then query it from your js. – S. Pellegrino Jun 16 '19 at 01:21
  • @S.Pellegrino That's true, and it's good that you pointed it out. Depending on op's needs, building a rest api on top of what he calls 'large Python program' might be a great solution too. – 89f3a1c Jun 16 '19 at 01:25
2

A couple ideas:

  1. Run a web server in your Python application and expose an endpoint that returns that data. In your Javascript app, call this API (accessible on 0.0.0.0) and consume the data. Caveat: you'll have to setup a web server in your Python app and keep it running.

Here's a better idea:

  1. Run a Redis instance on your Pi. Redis is an in-memory cache provider, and you can configure it not to persist any data to the disk. As such, it'll become an in-memory broker between your Python and Javascript applications. Redis runs as a local web server on your Pi and exposes endpoints for writing and reading data. From your Python app, call the Redis endpoint to write some data, and from your Javascript app, call Redis to read what you need.

https://redis.io/topics/ARM

Arash Motamedi
  • 9,284
  • 5
  • 34
  • 43
  • I like the approach of having an intermediate option which doesn't directly interfere with any of the two already existing parts. Although I don't know redis in detail, seems to be a good option. – 89f3a1c Jun 16 '19 at 01:35
0

You can pass the variable in the URL.

Using JavaScript, you can extract URL parameters. See this response for instance: https://stackoverflow.com/a/50769370/2223027

pyb
  • 4,813
  • 2
  • 27
  • 45
  • As far as we know from the question, there's no url. If there was, I don't think parsing a request would be a good option, unless the endpoint itself makes sense, but getting data from an endpoint is not what op asked. I don't think your answer addresses the issue. – 89f3a1c Jun 16 '19 at 01:21
  • @89f3a1c Pages have a `document.location`, why do you say there's no URL? According to "Is there a way to store this Python variable in memory (or some other way) which could be accessed by the Javascript code?", it looks like the value may not change often, so passing it as a URL might be an option. If not, I agree the Redis approach makes more sense. – pyb Jun 16 '19 at 02:08