Can you turn a website into a program?
July 26, 2009 7:06 PM   Subscribe

Is there a way to embed a rendering engine (like webkit), mysql, and php into a program that talks to itself, not the internet?

I'm almost certainly using the wrong terms. But imagine a php script that reads the text "hello world" from a mysql database.

How do I turn that into an executable that launches an embedded rendering engine, runs the embedded php script, queries the embedded database, and opens a window to display it, all without ever trying to leave its sandbox/communicate with the OS via a port?

Is it possible? If so, is it possible with more complex pieces of work like a CMS or a gallery script (waving away any other dependencies) or an etc.?
posted by Number Used Once to Computers & Internet (16 answers total) 1 user marked this as a favorite
 
In a word, I would say yes. However, there are certainly caveats. The main problem is that the task you're describing is typically carried out by many different programs working in concert: web browser --> server (over port 80) --> PHP CGI --> MySQL --> PHP CGI --> server --> web browser. It would (probably) be relatively simple to link in all these different services into a single executable.

The hardest part that I can see would be eliminating the client/server communication model (using ports) altogether. It would be far easier to let the various programs use their default communication mechanisms. How strict do you need your sandbox to be?

It's possible to have a PHP script dump its output to an HTML file, which might help alleviate some of the dependencies (since the rendering engine could just display the HTML file directly instead of requiring a server/CGI access). However, I'm not sure how this would scale with the complexity of the site (it's possible you could write a wrapper and be done, but I don't know for sure). I found this, which might point you in the right direction on that front.

One problem with the PHP --> HTML approach is that it doesn't resolve the PHP/MySQL dependency, since MySQL tends to run as a separate program/daemon. I'm not sure the most direct path to resolve this, so I'll leave it as an open question. If you're not married to MySQL, you might look into PHP/SQLite, which shouldn't require an SQL daemon.
posted by stufflebean at 7:29 PM on July 26, 2009


I do this all the time. Except that I don't use webkit, php, or mysql.

The only one on your list that's impossible to embed is mysql. It runs as a server and there's just nothing you can do about it. But, there are lots of embeddable databases. Everything from Berkley DB on up to Hypersonic SQL Server.

Every HTML rendering engine I know of will happily take markup from any source. So adapting gecko or webkit to work with stuff pulled from an embedded database should be trivial.

Embedding PHP is possible. However, it's a pretty terrible language to embed since most of its functionality is based on being intermixed with HTML. Likewise, it's going to take a lot of work to teach it how to connect to your embedded database (which will be embedded in your executable, in its execution space, not in the PHP space).

Personally, I do this in Java. It already has a (fairly basic) HTML renderer built in that's met all of my needs, and embedded databases are a dime a dozen. If I need scripting on top of that, I embed jython. I've also done it in Python, before, using

The LAMP stack, however, is atrocious for making local applications. [Not counting the utility of just connecting to local loopback, which can make for decent (if hugely bloated) local apps.]

I could give you better advice if you'd more precisely describe what you're trying to accomplish.
posted by Netzapper at 7:34 PM on July 26, 2009


Ooops.

Should read: "I've also done it in Python, before, using temporary files and the system browser as the display engine."
posted by Netzapper at 7:36 PM on July 26, 2009


If you're working with less than, say, 500 items, I'd dispense with the complexities of creating an application with all of these technologies embedded into it and do it all in javascript and flat HTML files with something like SIMILE's Exhibit script library, or for a more heavy-duty library something like dojo.data or YUI Datasource. Though that assumes that the system it would run on would have a web browser installed.
posted by XMLicious at 7:52 PM on July 26, 2009


There are two prominent tools for writing desktop applications using HTML-based interfaces: AIR from Adobe and Appcelerator Titanium. They're pretty similar at a high level- both allow you to create the an application whose interface is rendered by Webkit, but without all the interface elements of a normal browser. They both also provide some neat APIs, accessible from Javascript, to do useful things that are often impossible in a browser, like reading and writing local files. In particular, both let you create and use SQLite databases. SQLite, if you haven't heard of it, is the undisputed king of embedded databases... without digressing too much, take my word that it's fantastic.

Neither of those tools provides a webserver... their developers expect that the dynamic portions of the pages displayed will be generated using Javascript. AIR is locked down tightly, but in Titanium would actually provide the flexibility and APIs to bundle a webserver and PHP interpreter if you wanted... I've actually seen people asking about this on their message boards. But since it sounds like you're considering this approach because you don't have a lot of software development experience and were hoping to simplify things by writing this as a webapp and maybe reuse some public PHP image gallery (or whatever) code, I'm pretty sure that embedding a webserver in Titanium is more of a technical challenge than you're going to be interested in.
posted by gsteff at 8:07 PM on July 26, 2009 [1 favorite]


Qt has WebKit included and would make this very easy for you. You can embed PHP or run it with pipes. You can't run mysql within your program's process, but you can start & control mysql from your program. But, as previously pointed out, there are plenty of embeddable DBs.

I'd kind of question the point of such a system though. It seems like a lot of trouble to do something you could just do directly & save yourself the effort and pain of making 1000 things work together.
posted by devilsbrigade at 8:08 PM on July 26, 2009


I agree that PHP makes a poor language for embedding, and would suggest Python as an alternative. Python itself includes a webserver in its standard library. That webserver doesn't have a very programmer-friendly API, but there are some pure Python wrappers around it, most prominently Cherrypy, that make it pretty pleasant to write pure Python webapps. Python 2.5 and later also embeds SQLite. There also are some good tools out there for packaging a whole Python application into a binary executable that embeds the Python interpreter, so that you can distribute a single self-contained EXE file (or the equivalents for Mac and Linux). You could thus write a Python program that starts the webserver for your webapp, and then opens the system browser to view it, as Netzapper describes. In addition, if you can guarantee that your users will have Google Chrome installed, you can use Chrome's "app mode" to hide all of Chrome's own interface elements, so that you're basically dealing with raw Webkit as you described.
posted by gsteff at 8:17 PM on July 26, 2009


We have an iPhone app that uses a webview to display a couple of html pages that are stored in the app, not in the web. There is no problem doing that. I'm not sure that that fully answers your question, but it is a partial existence proof.
posted by alms at 8:50 PM on July 26, 2009


Although they're not tidily packaged, Fever and its sister app Mint are kind of like that: they run on a webserver running on your desktop, and you access that in your browser.

There are packages that put a MAMP or LAMP stack all into one app. That would leave out the browser, but in theory it seems like it would be possible to roll that in too, so that you'd have one part of the app talking to the other, rather than separate apps.

Also, the general principle of treating windows like web pages is increasingly common. All of the chrome in a Firefox (or other Mozilla app) is laid out using something very similar to HTML+CSS. I've heard that some of Apple's newer apps are doing the same: using Webkit to generate the chrome.
posted by adamrice at 9:12 PM on July 26, 2009


You can very easily do it by running a local server. If you're willing to code in Python, you should also check out http://www.aclevername.com/articles/python-webgui/
posted by miniape at 9:24 PM on July 26, 2009


Oh, another little note is that if you were willing to make your application Windows-only and IE-only, you could have flat HTML files (without a web server) containing VBScript that can connect to a Microsoft Access .mdb database file. The Microsoft Access "Jet" database driver that allows .mdb files to be used is installed in Windows by default.
posted by XMLicious at 10:07 PM on July 26, 2009


We have an iPhone app that uses a webview to display a couple of html pages that are stored in the app, not in the web. There is no problem doing that. I'm not sure that that fully answers your question, but it is a partial existence proof.

The reason you aren't sure if it fully answers the question is because the question isn't fully formed.

Nonce, you really need to tell us exactly what you're trying to accomplish, and on what platform(s).
posted by secret about box at 10:35 PM on July 26, 2009


It's worth noting that dojo works with Adobe Air apps, see this example.
posted by bertrandom at 1:38 AM on July 27, 2009


I do this on several applications that I've made with CherryPy (WSGI (html) server), SQLite (database), and browser (with jQuery for cool in-browser effects).

These are mostly simple CRUD applications that I use to make my life easier at work. And I probably wrote the first one (in Python of course) about 4 months after I started learning (as a never-programmed-before language learner).
posted by i_am_a_Jedi at 3:00 PM on July 27, 2009


Contrary to the above comments, MySQL does not have to be a standalone server. There is an embedded version. It's used by Amarok, for example.
posted by PueExMachina at 9:28 PM on July 28, 2009


Response by poster: I kind of got distracted by other, shinier things in my life. Thanks for all the answers. Basically, I've got a Wordpress theme that I run on a local server on my machine that acts like a personal information manager, with my diary, contacts, etc. all up in it. Started as an exercise, but became actually useful.

These have been really interesting answers. They make me want to start really programming. Thanks!
posted by Number Used Once at 11:24 AM on August 27, 2009


« Older EV RE50N/D-B mic + Olympus LS-10 recorder = hollow...   |   Where to major in film and the sciences? Newer »
This thread is closed to new comments.