Importing libraries such as networkX, numpy, matplot ...

711 views
Skip to first unread message

James Rolfe

unread,
Jul 21, 2015, 9:06:45 AM7/21/15
to bry...@googlegroups.com
I am trying to embed python code into an html file so that I can display properties of a various graphs using NetworkX, numpy, and matplot. However, I have found that I cannot import ANY libraries without the code crashing. If I try to import a library the code will crash before the "imports successful" line. Is there something I am forgetting to do that allows me to import libraries???

I have tried commenting out each library individually and I have tried to just import decimal, each time it crashes at the line marked below.

code snippet:

<!DOCTYPE html>
<html>
<head>
<title>FindNetProps_Brython_v1</title>
<link rel="stylesheet" href="UIKit/css/uikit.css" />
<script src="jquery.js"></script>
        <script src="UIKit/js/uikit.js"></script>
        <script src="Brython3.1.3-20150514-095342/brython.js"></script>
    </head>
    <body onload="brython()">
    <script type="text/python">

from browser import document, html, alert
import networkx as nx
import glob
import decimal
import matplotlib.pyplot as plt
import numpy as np

############ will crash before getting here ##########
document <= "imports successful"

def printProperties(filename):

inputFile = open(filename, "r")

myString = ''#initialize string to hold link pairs
myList = inputFile.readlines()#read all lines from inputFile and store each line individually in myList list
length = len(myList)
for i in range(length):
myList[i] = myList[i].replace('\n', ' ')#replace returns with spaces
myList[i] = myList[i].replace(',', ' ')#replace commas with spaces
myString = myString + myList[i]

mySinglesList = myString.split(' ')#remove spaces and store in new list
mySinglesList.remove('')#remove the additional return void that each "_as_link_pairs.txt" file has
G = nx.Graph()#create graph using networkx module
tupleList = zip(*[iter(mySinglesList)]*2)#turn the singles list into a 2-tuple list
G.add_edges_from(tupleList)#add all the edge pairs to graph G <<< will only allow passing of 2-tuples or 3-tuples!
document <= ('\nNumber of NODES in network: ' + str(G.number_of_nodes())) # capable of returning number of nodes after just adding edges
print('\nNumber of distinct EDGES in network: ' + str(G.number_of_edges()) + '\n') # function knows how to handle (0,1) and (1,0) case!

total = 0
degreeDict = nx.degree(G)#returns dictionary of degrees, keys are stored as string(ints)
print("Each node identifer on the left with the corresponding degree on the right: \n")
for i in range(G.number_of_nodes()):#iterate over the number of nodes, zero as first node
print(str(i) + ' : ' + str(degreeDict[str(i)]))
total += degreeDict[str(i)]
#ycoords[i] = degreeDict[str(i)] <<< #used to plot each node with corresponding degree

print()#return for readibility
min_val = min(degreeDict.values())#get min number connectivity
print("Minimum Degree: " + str(min_val))
max_val = max(degreeDict.values())#get max number connectivity
print("Maximum Degree: " + str(max_val))
realAvgConnectivity = float(decimal.Decimal(total/G.number_of_nodes()).quantize(decimal.Decimal('.01')))
print("Average Degree (my function): " + str(realAvgConnectivity))
print()#return for readibility

xcoords = list(range(G.number_of_nodes()))
ycoords = list(range(G.number_of_nodes()))

for i in range(G.number_of_nodes()):
ycoords[i] = 0

for i in range(1, max_val+1):
for j in range(G.number_of_nodes()):
if degreeDict[str(j)] == i:
ycoords[i] = ycoords[i] + 1

print()
print(ycoords)
print()

inputFile.close()

centralityDict = nx.degree_centrality(G)
for i in range(G.number_of_nodes()):
centralityDict[str(i)] = float(decimal.Decimal(centralityDict[str(i)]).quantize(decimal.Decimal('.01')))
print("CENTRALITY (networkx function):\n" + str(centralityDict)) # output also doesnt make sense ??????
print("Degree Assortivity (networkx function): \n" + str(nx.degree_assortativity_coefficient(G)))

#nx.draw(G) # draws graph showing edges and nodes

plt.figure(1) # declares "figure" which allows subplots to be made
ax = plt.subplot(111) # declares which numrows, numcols, fignum where fignum ranges from 1 to numrows*numcols 
plt.plot(xcoords, ycoords, '-bo')
plt.xlabel('Degree')
plt.ylabel('Number of Nodes')
plt.title('Degree Distribution of Network')
ax.set_xticks(range(G.number_of_nodes()))
ax.set_yticks(range(G.number_of_nodes()))

plt.grid(True)
plt.savefig('path.png')
plt.show() # show graph

'''
@pre: gets passed two strings. The first string should be of the format 'x,y\n' wher x,y are integers. The second string should be a .txt filename
@post: removes listed edge from passed file and appends '_MODIFIED' to the filename if not already there
@returns: boolean value
'''
def removeEdge(edgeToRemove, filename):
oldFile = open(filename, "r") # open file in read only mode
isModifiedBool = isModified(filename) # checks to see if filname has '_MODIFIED' in it
noExtensionList = filename.split('.') # split off .txt extension
noExtension = noExtensionList[0] # take string without .txt extension
if isModifiedBool:
newName = noExtension + '.txt' # adds .txt extension back on if already modified
else:
newName = noExtension + "_MODIFIED.txt" # append '_MODIFIED' to filename with .txt extension
newFile = open(newName, 'w') # create or open file in write only mode
oldList = oldFile.readlines() # read line by line the old .txt file, CAUTION: each line include '\n' at the end
edgeFound = False
complementEdge = findComplement(edgeToRemove) # find the complement of the passed edge to insure complete removal
for i in range(len(oldList)):
if oldList[i] == edgeToRemove: # check to see if edges match, if they do then do NOT write it to new file
edgeFound = True # boolean to confirm the edge was removed
continue # continue to next for loop iteration
elif oldList[i] == complementEdge: # also take out the complement of the edge >>> example: 'x,y\n' = 'y,x\n'
continue
else:
newFile.write(str(oldList[i]))
oldFile.close()
newFile.close()
if edgeFound:
return True
else:
return False

'''
@pre: takes string, should be name of .txt filename
@post: checks to see if filename has _MODIFIED appended to it, artificially throws an error to accomplish it
@returns: boolean value
'''
def isModified(filename):
isModifiedList = filename.split('_') # file format should be city/location_matrix_as_link_pairs_MODIFIED.txt
try:
if isModifiedList[5] == 'MODIFIED.txt': # throws 'out of bounds' exception if NOT modifed
print("\nis modified") # can be taken out
return True
except Exception as e:
print('\nnot modified') # can be taken out
return False

'''
@pre: takes a string, should be of the format 'x,y\n' where x and y are integers
@post: none
@returns: a string of format 'y,x\n' where x,y are integers
'''
def findComplement(edge):
noReturn = edge.replace('\n', '')
noCommaList = noReturn.split(',')
complementEdge = noCommaList[1] + ',' + noCommaList[0] + '\n' # note that 0 and 1 are switched then , and \n is appended back on
return complementEdge

'''
@pre: takes two strings. The first string should be of the format 'x,y\n' where x,y are integers. The second string should be a .txt filename
@post: adds passed edge and its complement to passed file in format 'x,y\n' where x,y are integers. Note: the edges will be added to the bottom of the file
@returns: boolean value
'''
def addEdge(edgeToAdd, filename):
try:
if edgeAlreadyExists(edgeToAdd, filename):
print("ERROR: edge already exists")
else:
oldFile = open(filename, "r")
oldList = oldFile.readlines()
isModifiedBool = isModified(filename)
noExtensionList = filename.split('.') # split off .txt extension
noExtension = noExtensionList[0] # take string without .txt extension
if isModifiedBool:
newName = noExtension + '.txt'
else:
newName = noExtension + "_MODIFIED.txt" # create new filename with .txt  extension
newFile = open(newName, 'w')
complement = findComplement(edgeToAdd)
oldList.append(edgeToAdd)
oldList.append(complement)
for i in range(len(oldList)):
newFile.write(oldList[i])
newFile.close()
oldFile.close()
return True
except Exception as e:
print("\nError when adding edge")
return False

##################### TESTING #########################
printProperties('Aarnet-tz_matrix_as_link_pairs.txt')
#######################################################

'''
@pre: takes two strings. The first string should be of the format 'x,y\n' where x,y are integers. The second string should be a .txt filename
@post: opens passed file and checks it for the passed edge
@returns: boolean value
'''
def edgeAlreadyExists(edge, filename):
oldFile = open(filename, "r")
oldList = oldFile.readlines()
for i in range(len(oldList)):
if edge == oldList[i]:
return True
else:
continue
return False

'''
@pre: takes two strings. The first is of the format 'x' where x is an integer. The second string should be a .txt filename
@post: removes all the edges from the passed file that contains the first passed string.
@returns: none
'''
def removeNode(node, filename):
oldFile = open(filename, "r")
oldList = oldFile.readlines()
isModifiedBool = isModified(filename)
noExtensionList = filename.split('.') # split off .txt extension
noExtension = noExtensionList[0] # take string without .txt extension
if isModifiedBool:
newName = noExtension + '.txt'
else:
newName = noExtension + "_MODIFIED.txt" # create new filename with .txt noExtension
newFile = open(newName, 'w')
for i in range(len(oldList)):
noReturnList = oldList[i].split('\n')
noReturn = noReturnList[0]
noCommaList = noReturn.split(',')
if (node == noCommaList[1]) or (node == noCommaList[0]):
continue
else:
newFile.write(oldList[i])

</script>
</body>
</html>

Joao S. O. Bueno

unread,
Jul 21, 2015, 9:56:05 AM7/21/15
to bry...@googlegroups.com
Libraries such as matplotlib and numpy are HUGE software projects with
megabytes worth of native code - it is not likely they will run
natively under brython anytime soon, nor that this will be practical
as an straightforward import, even when transparent compilying native
code trough LLVM (ala Skulpt) is (and if it is) ever implemented.

You can write and use native Python code - it is conceivable that with
some work on on the network libraries and extensive use of HTML5 ,
things like "Flask" come to work directly within Brython.

For what you desire, I think the natural approach is to have a server
side part of the application that can run the desired numpy and
matplotlib functions as remote-procedures. Your Brython code would
then run those remotely on the server and have their result via
json/ajax calls.

(Note that it is easier said than done - but it is possible to have a
thin layer that will make any server-side Python library avaliable via
rpc to Brython code, with relatively little effort - the downside is
that your server resources will have to scale--up to meet the
application demand)
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To post to this group, send email to bry...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/28686db3-b1fa-4e6e-abbe-08c4fae7323d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

André

unread,
Jul 21, 2015, 1:56:26 PM7/21/15
to bry...@googlegroups.com


On Tuesday, 21 July 2015 10:06:45 UTC-3, James Rolfe wrote:
I am trying to embed python code into an html file so that I can display properties of a various graphs using NetworkX, numpy, and matplot. However, I have found that I cannot import ANY libraries without the code crashing. If I try to import a library the code will crash before the "imports successful" line. Is there something I am forgetting to do that allows me to import libraries???


Excluding pure python libraries, each Python implementation can only run a subset of existing libraries.

 
Brython is a Python implementation written in Javascript and running in your browser.  Your browser can only run programs written in Javascript: Brython must first translate any existing *Python* code into Javascript before it can be used.  So, Brython code can make use of 1) pure Python libraries; 2) Javascript libraries.   It can not run numpy, matplotlib, etc...

CPython is the canonical Python implementation, written in C (with some modules written in pure Python).  It can run programs written in Python and interface with some other libraries (written in C, C++, Fortran...) provided someone wrote the appropriate bridging code for them and have them compiled. It can not make use of Javascript libraries, nor libraries based on JVM or .NET frameworks.

Pypy is a Python implementation written in (restricted) Python; it can run any pure Python program and has some (expanding) support for libraries written in C, Fortran, ...., similar to what CPython has, but does not yet offer the same support.  A tremendous effort is currentlybeing made to have it support numpy and related libraries.

Jython is a Python implementation written in Java.  It can run pure Python programs and make use of various libraries based on the Java Virtual Machine.  However (http://www.scipy.org/scipylib/faq.html#does-numpy-scipy-work-with-jython) "Jython runs on top of the Java Virtual Machine and has no way to interface with extensions written in C for the standard Python (CPython) interpreter" ... and thus can not run Numpy, matplotlib, etc.

(Leaving out description of IronPython, which is .NET based,  skulpt, etc.).

André

Olemis Lang

unread,
Jul 21, 2015, 2:07:50 PM7/21/15
to bry...@googlegroups.com
On 7/21/15, André <andre....@gmail.com> wrote:
>
>
> On Tuesday, 21 July 2015 10:06:45 UTC-3, James Rolfe wrote:
>>
>> I am trying to embed python code into an html file so that I can display
>> properties of a various graphs using NetworkX, numpy, and matplot.
>> However,
>> I have found that I cannot import ANY libraries without the code crashing.
>>
>> If I try to import a library the code will crash before the "imports
>> successful" line. Is there something I am forgetting to do that allows me
>>
>> to import libraries???
>>
>>
[...]
>
>Excluding pure python libraries, each Python implementation can only run a
> subset of existing libraries.
>

The most straightforward approach consists in using existing
client-side js charts libraries ... but still it's reasonable to push
towards a solution compatible with matplotlib . In my experience all
courses I've seen about numerical topics (machine learning , signal
processing , ...) rely upon numpy , scipy and matplotlib (most of the
time running inside ipython notebook) .

[...]

--
Regards,

Olemis - @olemislc

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound
http://blood-hound.net

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Kiko

unread,
Jul 21, 2015, 4:49:46 PM7/21/15
to bry...@googlegroups.com
it seems that for matplotlib 2.0 there will be some kind of (json)
serialization so maybe it will be easier to interact with it on the
client side.

In the meantime you can use js libs like highcharts, highstocks, d3,
raphäel,..., to plot with brython. See examples in the brython [1]
gallery or in the brythonmagic repo [2].

[1] http://brython.info/gallery/gallery_en.html?lang=en
[2] https://github.com/kikocorreoso/brythonmagic
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To post to this group, send email to bry...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/CAGMZAuNOQ9MQo-ssA3nXuPAx8FNHMqS3rLxQZ1%3DwK4icE0Kbzg%40mail.gmail.com.

James Rolfe

unread,
Jul 22, 2015, 10:52:53 AM7/22/15
to brython
So basically the collective saying is that it is not possible to use Brython to import and use NetworkX, matplot, and numpy. 

Thank you all for the quick responses. I think I will now pursue a django framework or maybe  I will try to understand how web sockets work

Francois Dion

unread,
Jul 22, 2015, 12:48:42 PM7/22/15
to bry...@googlegroups.com
Even good old matplotlib no longer is the end game. There is http://mpld3.github.io for the web (using d3). And if you kept tab on Scipy 2015, http://vispy.org is quite interesting on the desktop...

Francois

On Wed, Jul 22, 2015 at 10:52 AM, James Rolfe <james...@gmail.com> wrote:
So basically the collective saying is that it is not possible to use Brython to import and use NetworkX, matplot, and numpy. 

Thank you all for the quick responses. I think I will now pursue a django framework or maybe  I will try to understand how web sockets work

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages