Save a pivottablejs figure to file - javascript

I have started using the package pivottablejs to manipulate and visualize pivot tables in python.
from pivottablejs import pivot_ui
pivot_ui(df) # where df is a pandas dataframe
will produce an interactive pivot table/plot in a jupyter notebook.
Is there any pythonic way to save the figures produced by this package to (say) png from within the jupyter notebook? I am looking for something similar to the classic plt.savefig('file.png'). The front-end is essentially javascript, and I do not know how (or if it is possible) to access a javascript figure through python.

This is too long for a comment, so I'm adding it as an answer. pivottablejs creates images as svgs and they do not provide any download/export mechanism for the images. But what you can do is inject js into the cell and download the svg using that.
One simple way of doing this is using svg-crowbar.js. After you generate the chart using pivot_ui and the output is displayed on the screen, run the following in the next cell to download the svg.
In [10]: %%javascript
var e = document.createElement('script'); e.setAttribute('src', 'https://nytimes.github.io/svg-crowbar/svg-crowbar.js'); e.setAttribute('class', 'svg-crowbar'); document.body.appendChild(e);
Note: This only works on chrome.

you can save it as html by specifying outfile_path
pivot_ui(df, outfile_path="yourpathname.html")

Related

How to write excel file with chart using js-xlsx

Using SheetJS js-xlsx "xlsx": "0.15.1" js is there a way to write excel file with chart.
Basically I want to export html page that contains chart into an excel file.
HTML page is getting exported successfully but the chart in the page is not getting displayed.
Here is the code I have used
let wb =XLSX.utils.book_new();
let element: any = document.getElementById('page1');
let ws = XLSX.utils.table_to_sheet(element);
XLSX.utils.book_append_sheet(wb,ws,'page1');
Are you using the Community build or the Pro?
As stated here, The Community build reflects the data cached in charts as worksheets. Other chart-related features, including writing and reading metadata, are included in the Pro compendium.
Remember to include your work as Chartsheet, just in case. It will continue to render it as a Worksheet if you are using the community version, but it will help us to understand the desired behavior that you are seeking.
If you provide more reference for the code, maybe we can show you some shortcuts to make it work.

R <-> JS: loop over XML nodes and return getComputedStyle values to dataframe

In a nutshell, I want to write some code that will take in a URL and return a dataframe with this page's HTML elements and their CSS values. Now my major problem is finding a way to make JS and R communicate.
At this point, I need to mention that I have a limited coding knowledge (i'm a marketer). I learn R and need to use it to complete the task.
I've found that "getComputedStyle" is the JS function I need, but the page must be open in the browser to return the actual computed styles.
Thus, I've come up with a solution below:
Creating some simple Chrome plugin that would send the current site's URL to R, R would create a list of XML node paths and in a loop, using querySelector and getComputedStyle, populate a dataframe with the CSS values returned by the plugin.
However, I don't know how to make JS and R communicate.
I need to make it work for me only so should I somehow run R on localhost (I use Mac OS)? And then what?
I've read a little about OpenCPU but am not sure if that's what I need.
Or maybe there's some other solution?

Display image using Nodejs only

I am trying to display an image by only using Node js. I have been searching for a way to do this, but the only examples that I can find involve HTML and ID's within the HTML which I do not want to use. Sorry I do not have any code to go along with my question, but is there a way to avoid all HTML and just use Node js or a specific node js module to store the image in a variable which would later be used to display the image?
store the image in a variable which would later be used to display the image
You can achieve this by having NodeJS print you the Data URI of the image. It will be a rather large block of string, so quite cumbersome, but certainly will let you fetch an image from NodeJS without using a web browser/HTML.
You can also launch an arbitrary image viewer of your choice by having NodeJS launch a child process.

Can I call a JavaScript function on an iFrame or similar widget from Python?

I am trying to mix Python code with D3 vizualizations. Right now I have a bunch of D3 vizualizations and JavaScript functions. I have a couple different ways of embedding the D3, but what I want is to call a function that affects the vizualization AFTER I create it.
For example:
createD3Stuff()
for i in [0,1,2,3]:
executeFunction(i)
Where createD3Stuff() generates the vizualization, and executeFunction does something on that vizualization, like creates an element with id=i.
Is this possible? How can I do it?
Background information: I am using Jupyter Notebooks, but if there is some other way to get this to work I can switch. I know how to do all the d3 and javascript stuff, it is just getting the python and javascript to interact that is giving me trouble.
Of course you can.
Just try this:
from IPython.display import HTML
HTML("<script>alert('hello world')</script>")

How to get LanguageModel JS File?

I have been using PocketSphinx.js for speech recognition on my website. I have Downloaded language model file (.dmp). But their code uses JS file for Language model. I dont know where to get JS file. Help me out. Thanks.
As per documentation, you can convert dmp file to js and load it afterwards, see the section https://github.com/syl22-00/pocketsphinx.js/#i-embedding-the-files-into-one-large-javascript-file. Something like
$ cmake -DEMSCRIPTEN=1 -DCMAKE_TOOLCHAIN_FILE=path_to_emscripten/cmake/Modules/Platform/Emscripten.cmake -LM_BASE=/lm/dmp/folder/name -LM_FILES=model.dmp ..
Also check an alternative section https://github.com/syl22-00/pocketsphinx.js#ii-package-model-files-outside-the-main-javascript where you can do:
# python .../emscripten/tools/file_packager.py .../pocketsphinx.js/build/pocketsphinx.js --embed model.dmp --js-output=model.dmp.js
Overall, you need smaller models, big models from CMUSPHINX downloads are too large

Categories

Resources