JavaScript Transfer array from python to javascript transfer array from php to javascript,transfer array in java

I have a test.txt file that has two columns of data (x's and y's), eg:

#test.txt
1 23
2 234
4 52
43 5
3 35

And a python program that reads in these values and stores them in x and y as so:

#test.py
# Read the file.
f = open('test.txt', 'r')

# read the whole file into a single variable, which is a list of every  row of the file.
lines = f.readlines()
f.close()

# initialize some variable to be lists:
x = []
y = []

# scan the rows of the file stored in lines, and put the values into some variables:
for line in lines:
    p = line.split()
    x.append(float(p[0]))
    y.append(float(p[1]))

I want to take these values stored in x and y and transfer them to two similar arrays in a javascript program to display them as a graph. How can I transfer between python and javascript?

Answer:1

Your question is slightly vague. There are two possible ways your question can be interpreted, here are the corresponding answers:

  1. Your python code needs to transfer them to some javascript running in the browser/node.js application. In order to do this, the Python half of your application would need to either store the data in a database and expose the data via some sort of API, which the javascript half could consume. To go a little fancier, you could set up a connection between the two web servers (e.g. socket.io) and have the Python half send the arrays over the connection as they're created.
  2. This is what I believe you're trying to do based on the code you've posted: Preprocess some data in Python, and then pass it over to some other javascript piece of the puzzle where there's no real-time aspect to it.

    In this case, you could simply write the arrays to JSON, and parse that in Javascript. To write to a file, you could do:

    import json
    data = {'x': x, 'y': y}
    # To write to a file:
    with open("output.json", "w") as f:
        json.dump(data, f)
    
    # To print out the JSON string (which you could then hardcode into the JS)
    json.dumps(data)
    
Answer:2

Its unclear as to why you want to transfer this, but you can write it out into a js file simply by formatting the list into var someList = [[x1,y1], [x2,y2]].

Would be easier to build the string up withing from line in lines but in case you do have actual need of maintaining separate x and y lists

zippedList = list(list(zipped) for zipped in zip(xList,yList))
print('var someList = {};'.format(zippedList))

which gives you

var someList = [[1, 23], [2, 234], [4, 52], [43, 5], [3, 35]];

Write this out to a js file as needed or append to an existing html as

with open('index.html','a') as f:
    f.write('<script> var transferredList={}; </script>'.format(zippedList))
Answer:3

Because you mention "display" together with JavaScript, I somewhat assume that your goal is to have JavaScript in a web browser displaying the data that your python script reads from the file in the backend. Transferring from backend python to frontend JavaScript is a piece of cake, because both languages "speak" JSON.

In the backend, you will do something like

import json
response = json.dumps(response_object)

and in the frontend, you can right away use

var responseObject = JSON.parse(responseString);

It is also quite clear how the frontend will call the backend, namely using ajax, usually wrapped in a handy library like jQuery.

The field opens, when it comes to making your python script run in a web server. A good starting point for this would be the Python How-To for using python in the web. You will have to consult with your web hosting service, too, because most hosters have clear guidance whether they admit CGIor, for example FastCGI. Once this is clear, maybe you want to take a look at the Flask micro-framework or something similar, which are offering many services you will need right out-of-the-box.

I have mentioned all the buzzwords here to enable your own investigation ;)

Answer:4

I'm currently working on something that requires me to put the value of an input field as an object item. var test = { item1:{ subitem1: "", <------- This is the one I'm trying to fill ...

I'm currently working on something that requires me to put the value of an input field as an object item. var test = { item1:{ subitem1: "", <------- This is the one I'm trying to fill ...

I have a situation where I need to add a class to a table, but only if the table is inside a div with id 'mydiv' AND there is a row in table containing an <a href=...> element where the href ...

I have a situation where I need to add a class to a table, but only if the table is inside a div with id 'mydiv' AND there is a row in table containing an <a href=...> element where the href ...

I want to exetend Tab component with es6 class like this: import React from "react"; import {Tab} from "material-ui"; class MyTab extends Tab { constructor(props){ super(props); } ...

I want to exetend Tab component with es6 class like this: import React from "react"; import {Tab} from "material-ui"; class MyTab extends Tab { constructor(props){ super(props); } ...

  1. extend material component
  2. angular extend material component
  3. material ui extend component
  4. extends angular material components

Is it possible in jquery to insert text after a span. For example, I would like to change the price value after the dollar sign below. <div id="price"> <span>$</span> 10.00 &...

Is it possible in jquery to insert text after a span. For example, I would like to change the price value after the dollar sign below. <div id="price"> <span>$</span> 10.00 &...

  1. changing text after effects
  2. changing text color after effects
  3. changing text colour after effects