librelist archives

« back to archive

Dispatching URLs through Flask without a TCP port

Dispatching URLs through Flask without a TCP port

From:
Juancarlo Añez
Date:
2011-10-13 @ 20:42
I want to build a hybrid application (Web technologies embedded in a desktop
app).

I will start with a Web version and the embed it using WebKit, but I don't
want the embedded version to service requests through a TCP port.

With WebKit (Qt,Gtk) I can intercept all URL requests and act on them.

What I'm missing is a way to invoke the Flask URL-to-callable dispatcher
without going through TCP (or WSGI).

Any ideas better than analyzing the call stack with a debugger?

Regards,

--Juancarlo

Re: [flask] Dispatching URLs through Flask without a TCP port

From:
Simon Sapin
Date:
2011-10-13 @ 20:58
Le 13/10/2011 22:42, Juancarlo Añez a écrit :
> I want to build a hybrid application (Web technologies embedded in a
> desktop app).
>
> I will start with a Web version and the embed it using WebKit, but I
> don't want the embedded version to service requests through a TCP port.
>
> With WebKit (Qt,Gtk) I can intercept all URL requests and act on them.
>
> What I'm missing is a way to invoke the Flask URL-to-callable dispatcher
> without going through TCP (or WSGI).
>
> Any ideas better than analyzing the call stack with a debugger?

Hi,

Why not WSGI ?

You have to get a Python interpreter somewhere. Then you need to call 
your application somehow with data from WebKit like the URL being 
requested, and get the response. WSGI is just that: a calling convention 
for Python functions (or other callable objects.)

If WSGI is more complex than you’d like, you can use the test client:

http://flask.pocoo.org/docs/api/#flask.Flask.test_client
http://werkzeug.pocoo.org/docs/test/#werkzeug.test.Client
http://werkzeug.pocoo.org/docs/test/#werkzeug.test.EnvironBuilder

That’s how I do it in Frozen-Flask. It simulates HTTP requests to a 
Flask app at the WSGI level and write the responses to static files. The 
test client is just an easier way to make WSGI calls:


https://github.com/SimonSapin/Frozen-Flask/blob/master/flaskext/frozen/__init__.py#L228

WSGI really is Flask’s "entry point".

Other than that if you’re interested in Flask inner workings start 
looking from here:

https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L1477

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Dispatching URLs through Flask without a TCP port

From:
Juancarlo Añez
Date:
2011-10-13 @ 21:51
Simon,

On Thu, Oct 13, 2011 at 4:28 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> WSGI really is Flask’s "entry point".


Thanks for such a useful answer.

Could you provide an example of using WSGI directly?

-- 
Juancarlo *Añez*

Re: [flask] Dispatching URLs through Flask without a TCP port

From:
Simon Sapin
Date:
2011-10-13 @ 22:05
Le 13/10/2011 23:51, Juancarlo Añez a écrit :
> Could you provide an example of using WSGI directly?

Hi,

The PEP 3333 defines WSGI and has a code sample for a CGI gateway:

http://www.python.org/dev/peps/pep-3333/#the-server-gateway-side

but CGI is "cheating" since many `environ` variables you need to set for 
WSGI are the same in CGI (coming from `os.environ`). See later in the 
same document for when you can/must set in there:

http://www.python.org/dev/peps/pep-3333/#environ-variables

If you want to go that way I strongly advice that you read the whole 
document, but again the test client might be easier.

Also note that code samples in PEP 3333 are in Python 3. See PEP 333 for 
the Python 2-only version.

http://www.python.org/dev/peps/pep-0333/

Regards,
-- 
Simon Sapin

Re: [flask] Dispatching URLs through Flask without a TCP port

From:
Simon Sapin
Date:
2011-10-13 @ 22:08
Le 14/10/2011 00:05, Simon Sapin a écrit :
> If you want to go that way I strongly advice that you read the whole
> document, but again the test client might be easier.

If this wasn’t clear: the test client itself uses WSGI. It just helps 
building an `environ` dict. (Or you can use only the EnvironBuilder part.)

-- 
Simon Sapin

Re: [flask] Dispatching URLs through Flask without a TCP port

From:
Juancarlo Añez
Date:
2011-10-14 @ 01:13
Simon,

On Thu, Oct 13, 2011 at 5:38 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> If this wasn’t clear: the test client itself uses WSGI. It just helps
> building an `environ` dict. (Or you can use only the EnvironBuilder part.)
>

Then there's no need to do it the "hard" way.

Thanks again!

-- 
Juancarlo *Añez*