Processing SVG in Python - javascript

I am trying to use python and understand SVG drawings. I would like python to behave similar to java script and get information from SVG. I understand that there can be 2 types of information in SVG.
XML based information - such as elementbyID, elementbyTagNames
Structural information - positional information taking transformations in to consideration too - such as getelementfrompoint, getboundingbox
I have searched around and found python libraries such as lxml for xml processing in svg. Also I found libraries such as svgpathtools, svg.path , but as I understand, these deal only with svgpath elements.
So my question is,
Are there any good libraries which support processing svg in python?(similar to java script)

I don't think this is feasible. I was directing you to an answer about rendering SVG with Python - but after that all you have got is pixels.
Extracting positional information from SVG data at arbitrary points between transformations is likely something only implemented in browsers themselves. You will likely have two options: use a headless browser and selenium/splinter to load your SVG data in a real browser and run javascript statements in there, OR make your Python code run in Brython, and run everything inside the browser. From Brython you should be able to use the Javascript calls as methods of the SVG object as it is exposed to Brython itself.

Try to use Pygal. It's used for creating interactive .svg pictures.

Thank you for all the answers and help.
After reading all comments and even more www search, I agree with the idea that there is much more support in Java script supported web browsers for dealing with SVG. So I decided to use JS and to use python only when there is no choice. I will however use python libraries such as(https://pypi.python.org/pypi/svgpathtools/1.0.1) I think. But as of now I handed over all the SVG feature finding to java script

Start your search by visiting www.pypi.org and search for "svg". Review what exists and see what suits your needs.

Related

How to use DCMTK library with javascript

After working with dcmtk in C++, I'd like to use it in javascript but I think it's not as easy as it is with C++.
is there any way to do that ?
thank you in advance.
I agree to John, I would rather advise to seek for a JavaScript DICOM toolkit instead of establishing an interface between DCMTK and JavaScript.
To answer your question, however:
First, make a basic decision if you want to use the toolkit's executables through kind of a scripting layer that is invoked through JS functions or to write C++ - CGI funtions based on the DCMTK libraries. I think it is obvious that the latter approach gives you far more flexibility in designing the DICOM functionality. In the following, I am going to mention executables which can accomplish particular tasks. In case you want to go the CGI way, the source code of the executable is a good starting point to learn how to use the library.
To read the DICOM header information, have a look at dcmdump. It can convert the binary DICOM header format into a text file which can be easily parsed with non-DICOM-aware JS functions.
To create binary DICOM objects, use the complementary tool dump2dcm, which converts a text file in the format that dcmdump creates back to a binary DICOM file.
To render images to a "web image format" (i.e. PNG or JPEG), you can use dcmj2pnm. It takes a DICOM image and renders it with some simple rendering functions (scale, rotate, windowing).
All of these tools provide a lot of options through the command-line interface to control the output.
There are more tools around which may be helpful but without knowing more about the use cases you want to support this is the information I can provide. Please note again, that I explicitly do not want to advise you to use these approaches, as they are very limited in terms of performance optimization and error handling.

c++ algorithm with html(/javascript) SVG graphics - few questions

I have an algorithm in c++ which create an 2D table with float values, basing on that values I want to create svg graphics in html(or javascrpit). And my question is - can i anyhow make it in one code (creating that 2D array in c++, keep it in memory and basing on that, creating graphics by html), or better(/only possible?) would be for example saving 2D array to .txt then creating separately .html code in which I open the .txt file, read values and then creating svg graphics (if it is possible, i'm totally green in html/javascript).
I hope You can give me some advice guys. :)
JavaScript doesn't have access to ANY memory management in the browsers. You could probably only connect with http requests.
However there are parsers like Rhino, which allow for Java classes to be made into javascript objects and reverse, I have yet to hear of a C++ one though.
NodeJS allows access to the filesystem, thus any files you save will be accessible and there might be an api for C++.
You could use Emscripten to compyle C++ into JS, but it is hard.
You can write the table as json file that can be easy interpretted by javascript.
Or, my recomandation, use a svg library in C++ and skip html. see: Render a vector graphic (.svg) in C++
or search google.
If you are more specific about the usecase you would get batter answers:
How will the application run: part of a web site or local
Why do you need HTML
Who will use this: you want to convert some data to SVG for a small project or some other users will use your program for a longer time.

On-Disk Text Processing With Javascript

I have some html files that I need to do automated processing on, basically regex replaces, but also some more complex actions like copying select blocks of text from one file to another.
I want to create a series of scripts that will let me do this processing (it will need to be done more than once on different batches of files). It would be trivial to use Go for this (read the file into memory, regex, save to disk) but I am the only member of the project that's familiar with Go.
Javascript is a tad more ubiquitous, and I do have project members who are familiar with the language, so it's a better fit in that respect. If I'm not around later, someone else could edit the scripts.
Is there a simple way to write some JS scripts to do on-disk text processing? I'm looking for a cross-platform solution (OSX, Windows). Ideally, once the scripts are written, they can be executed by double-clicking an icon--there will be "not computer people" involved at some point.
Also, I'd like to be able to do some kind of alert/message box to inform the user of the success/failure of the script. (This may be a tall order, and is of secondary importance.)
What I've looked at:
Node.js was the first thing that popped into my head, because I know that it has file system access tools, and obviously regex capacity. But I've never used Node before, and based on the tutorials I've read, it seems like overkill for something this simple.
There's a whole slew of "javascript compiling" tools that you can find by googling around. Some are not cross-platform, some seem old or not actively maintained, etc. None of them caught my eye as easy to pick up and just write some JS scripts with.
Any thoughts?
Node.js is a simple solution and with it's framework you can create or later modify your script to your needs. This way you will not be locked down by someone else's code. And it is not that difficult to to use.
Here is a quick tutorial on accesing files using node.js
http://www.sitepoint.com/accessing-the-file-system-in-node-js/
And here is a quick tutorial on using a node module called Cheerio. It allow you to access html files using "jquery like syntax". You don't need to use regex.
http://maxogden.com/scraping-with-node.html
I worked on a project for a client once and it required parsing thru hundreds of html files to check and replace certain image files based on certain criterias. I wasn't familiar with node at the time so I read some tutorials and wrote the script in about an hour.
And as long as Nodejs' path is set, you can run it on the command line.
Some tips:
You need any kind of DOM HTML parser, not only JS nor specifically JS.
You can do that thing with Java with use of jTidy or jSoup libraries (I've used second one few times). It's pretty simple language to learn if you know JS and IDE like Netbeans helps a lot. So can be made quickly with that.
You can use PhantomJS to create some job files and create shell/batch code to run them on some files. You might need to write a generator for job files (like taking a list of files, creating job files for each and running them).
You can use Node.js which isn't much overkill, I'm sure any solution won't be trivial.
You can create an ETL for processing with for example Pentaho ETL (which has JS embedded as one of two scripting languages... but without DOM parser - for that one you would need to use a bit of Java there and some library in way similar to this article).
You can also do that with PHP with Simple HTML DOM Parser - so you can make a service online (or on local server) that takes those html files and throws out processed ones.
First I think you underestimate the complexity. The statement
"It would be trivial to use Go for this (read the file into memory,
regex, save to disk) but I am the only member of the project that's
familiar with Go."
is probably false. Parsing HTML with RegExp is just a bad idea. (Google it and you will see why)
Second, if you can trivially write the code using RegExps in Go, you can just as easily write the same thing in Javascript. They both support RegExp and file operations. If you are unsure about the Javascript/Node.js details, I suggest writing the trivial solution in Go and then translate the thing into Javascript with a colleague.
Since Javascript is a script language, writing command line utilities in Node.js is straight forward.
Some pointers to get you started
RegExp in Javascript
Building command line apps in Node.js

How to use JavaScript to stream parts of a file?

Given a really large, 3+ gig, binary file is there anyway that I could stream, from client to server, only portions of the file using JavaScript given that I know what byte range of the file that I want to receive?
I have a Ruby on Rails application that needs to grab specific portions of a file from the client. As one user has stated I could do this using Java.
Edit: After some reading it appears that HTML5 via slicing a file may be the best bet. http://www.html5rocks.com/en/tutorials/file/dndfiles/
The basic answer is yes, assuming your web server supports it (which many do).
You can use the Range HTTP header to request only a part of the file (e.g. Range: bytes=1000-2000). Whether this works for you depends heavily on what you’re trying to accomplish — more information would help.
See this answer for a discussion on using it.
No, not really (at least not now, anyways). The file handling capabilities exposed to Javascript is not powerful enough to really do anything useful client-side when processing files to send back to the server (including things like only take part of a file). There are proposed w3c specs for better client-side file handling for javascript, but none of the major browsers implement it to a sufficient level to really handle this case quite yet.
I'm currently working on a project with similar needs, and the only options we found when we looked into this was to either use Flash, or to use Java. Since we are much more comfortable with Java than flash, we went that route.
We are currently using Groovy and the Griffon framework, as well as Grails for the server-side pieces. Griffon has been great because it frees us from the hassles of desktop vs. webstart vs. applet, and since it's built on Groovy, it can leverage the Swing DSLs so it is much less painless to write Swing.

Best way to design cross-platform multi-content-type file format?

I need to redesign a file format for a set of applications ( desktop/online/mobile.. ) which is to contain a set of non-linear data and multiple images.
I do not want to make it one large XML with embedded images as:
first of all it could grow to a size that would be for example slow to parse using javascript
second of all I want it to be editable 'by hand'.
The compression is not necessary. I just want to distribute this as a single files. I would like my solution to be as cross-platform as possible.
What I made up recently is a *.tar.gz archive with xmls and images inside.
The desktop (C++ / Qt) version of my application is reading the file using zlib (which was hard to code but works).
The online version is using PHP (execing tar) to uncompress the file (caches contents in database) and passes xmls and images to javascript via ajax.
When I came to develop an android version I'm kinda stuck and I don't know how to read it here.
I was wondering if there is a better / simpler solution to achieve this?
Solutions I found are for example:
Blender .blend files http://en.wikipedia.org/wiki/Blender_%28software%29#File_format But this is not readable without any special tool.
OpenOffice files which are in fact zips.
But I guess there is no big difference using this or tar.gz. It still would be a problem to read it on a mobile platform or with javascript.
Probably there is no 'best way' to do this but i hope there are some widely used solutions?
I will appreciate any thoughts on this.
I'm not sure if this will meet your "editable by hand" criteria, but to make a single-file, cross-platform binary format, Google Protocol Buffers are very useful and efficient, and work across major platforms and languages.
If "editable by hand" is critical, with a little extra work, you can write simple utilities using your own message libraries to support creating/updating/deleting portions of existing files (e.g. extract out images for editing, and support replacing them.)
If it must be editable in JavaScript, perhaps JSON might be a good option? You can insert pictures/video/other files by using base64 data url encoding, which is supported in Javascript and many other languages.

Categories

Resources