Python + Webkit-based GUI? - javascript

Is it possible to create a python script (eg. main.py) which creates the GUI window, which loads a .html file (with necessary css and possibly javascript), and have this .html file GUI interact with the main.py?
For example, in the .html file the user might click a button, which needs to interact with a function in the main.py file, and return a result in the .html file (python probably cannot edit the html dom, but may return a result the javascript can?).
I read PyQt5 can create a webview (load html files locally or externally), but I need it to interact with python for returning results.
Thanks!

Related

Can't load Javascript library: ERR_NAME_NOT_RESOLVED

I have an Apache web server where I must display some graphs and I must be able to do it offline. I have a couple of js libraries I must use in order to render the graphs and when I load my index.php page, it gives me GET http:d3.js/ net::ERR_NAME_NOT_RESOLVED, but the file is in the right folder and I've copied and pasted the code in index.php in a test file and when I open it, it works just fine.
The html for the graphs is being generated using the MPLD3 function of fig_to_html, this function generates an html string, that I append to my index.php, the path to the js libraries is also specified in that function:
mpld3.fig_to_html(fig, mpld3_url='file:///home/pi/webpage/mpld3.js, d3_url='file:///home/pi/webpage/d3.js')
Does not seem a valid file url:
http:d3.js/
Try "d3.js" or "/[path-on-site]/d3.js".

Javascript file input

I am trying to make a JS program using a text file on the client side. The thing is that it was difficult to find out how to do such a basic functionality because it is not a desired feature. I want to open a text file which is not in the local directory of a user but in the directory with the html file. So, what I want is the JS version of this code (which is in python)
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n");
# Close opend file
fo.close()
Thanks!
You can read a file with FileReader() (as a result of a user selecting files) but fortunately we can't modify client's or server's files with JavaScript in client side (there is no FileWriter()). To do that, you should upload the file to the server (through Ajax or a simple html form) and modify it with server side code.

Loading an XML locally into JavaScript without having a web server available?

I've made an script for processing XML:
Extracts parts of the XML-data and displays them on a HTML-/CSS-page.
Currently I use the stringified content of some arbitrary file for doing my development. Written as a string-literal into my JavaScript.
Works great. But now comes the problem:
Of course I would like to load whatever (equal-structured) XML-files. Instead of having it in my code as a string-literal.
Normally I would load the files into my script via Ajax.
But I can't install a web-server on these computer.
I'm within an enterprise and it isn't possible to install any additional software. Restricted via group-policies etc. No chance. Forget it!
As far as I know it isn't possible to use Ajax without a web-server because Ajax communicates via the http-protocol.
So here's my questions:
I there (perhaps) a possibility to use Ajax without a web-server?
And in case of impossible:
Have I got any Ajax-alternatives to load XML-data into my script?
You can embed your xml inside script tag in your html like this:
console.log(document.getElementById('file').innerHTML)
<script type="text/xml" id="file">
<root><foo><bar></bar></foo></root>
</script>

running .bat file with javascript on html

I need to run a .bat file when the user clicks a link on the webpage. The .bat file basically runs another executable file and saves its output in a text file. So what I want is to create that text file once the user clicks the link to the .bat file on the webpage. Now, my .bat file is working perfectly when I execute it separately it creates the text file with contents, but somehow when I click the link it creates an empty text file. I looked at all the paths, they are all good;
I am using
Batch File ,
I have also tried
function runApp(which)
{ WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run (which,1,true);
}
But both of them just create the text file, and not put the contents
Does any one has any idea about this, also is there any other way to do this, like running the original .bat file and then getting its output in a text file directly with html/javascript?
Appriciate any help
Thanks
You don't say anything about what environment you are working with and I would guess you're not working with a server-side environment. JavaScript normally works in a browser to respond to the user's clicks and mouse moves etc but strictly within the confines of the browser. What you are trying to do is perform I/O operations on the underlying OS that the browser is running in (if you are running locally) or on the server-side OS in a normal webpage environment. It's not just a security issue - JavaScript simply doesn't have any direct connection to the client's OS or the server-side OS for that matter.
What you need is a web server environment like Apache or IIS etc, probably running an environment like ASP.NET, JSP, PHP(with a nice framework like CodeIgniter), or, rather you than me, CGI.
The user clicks a link or a submit button, and sends a request to the server. The relevant server-side program processes the request, runs the I/O operation you talk about and responds with the text. JavaScript is irrelevant in most of that process. It only comes into its own again when you are trying to figure out how to display the response in some fancy dynamic way.
There are millions of tutorials out there:
Tomcat (Java) http://wiki.apache.org/tomcat/GettingStarted
.NET(C# or VB) http://www.asp.net/get-started
Codeigniter (PHP) http://codeigniter.com/
CGI (not for the faint-hearted) http://www.cgi101.com/book/ch1/text.html
Having said all that, there is a server-side JavaScript environment (http://nodejs.org/) but the point is you will always be restricted by the limitations of the http protocol which means that you send a request to a server, the server processes your request depending on your privileges as a client, performing an I/O operation if appropriate, and responds with a stream of HTML. It does not allow direct operations on the server.
None of this is easy. Expect steep learning curves.
Displaying the text file contents
Here's a sample JSP page which will read the contents of a text file then display it on the webpage. I haven't added any buttons or anything - it just demonstrates how to read a file on the server:
<%#page contentType="text/html" pageEncoding="UTF-8" import="java.io.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BufferedReader</title>
</head>
<body>
<%
String path = this.getServletContext().getRealPath("/target/message.txt");
File file = new File(path);
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
while(br.ready()){
out.print(br.readLine() + "<BR>");
}
reader.close();
%>
</body>
</html>
/target/message.txt is the virtual absolute path (from the root of the webapp). The call to getRealPath is the way you get a real physical path that allows you to create a File object.
I'll have a look later at using exec to run a batch file, but now you're thinking of a powerful language/library like Java why do you want to run a batch file? Wouldn't it make sense to write the program in Java?
use child_process.spawn of node.js

How can I print the contents of a js file to the screen from a URL?

I have an application that will check if a javascript file exists on our CDN and I would like to display the contents of that file in the browser window. I have all the nuts and bolts figured out, I just need to display the contents of the javascript file when I put in the URL where it exists.
Thanks in advance.
I believe you can output a javascript file to a webpage by just making sure any html elements are escaped. You can use the htmleditformat() function to do this when you output the value.
For example:
<cfhttp url="#jsurl#" />
<cfoutput><pre>#htmleditformat(cfhttp.filecontent)#</pre></cfoutput>
Other options just using the js address directly are to 1) link to the js file if you just want to be able to see it in the window, or 2) show the js file in an iframe
I'm not quite sure what you're asking:
"I need to open a javascript file in a web browser"
A. this is OS dependant, you call the binary that's registered for .htm files with parameters pointing it's initial destination to that .js file.
"I have a web app and want to dynamically display the contents of remote files formatted as text"
A. Use a block and fill it with GetWebResourceUrl().
"I am writing a standalone app and want to display remote files as text"
A. If you are writing a standalone, why do you want to display the contents of a js file via a browser? Use your local API, or launch the default text viewer with a local temp copy.

Categories