This file is indexed.

/usr/share/doc/python-myghty/html/programmatic.html is in python-myghty 1.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<html>
<head>
	<title>
Myghty Documentation
</title>
	<link href="style.css" rel="stylesheet" type="text/css"></link>
	<link href="syntaxhighlight.css" rel="stylesheet" type="text/css"></link>
</head>
<body>




<link href="docs.css" rel="stylesheet" type="text/css"></link>


<div style="position:absolute;left:0px;top:0px;"><a name="top"></a>&nbsp;</div>

<div class="doccontainer">

<div class="docheader">

<div class="docheadertext" >Myghty Documentation</div>
<div class="">Version: 1.1   Last Updated: 09/10/06 19:32:29</div>
</div>



		<A name="configuration_programmatic"></a>
		
	
<div class="topnav">


	
	<div class="topnavcontrol">
	View: <b>Paged</b> &nbsp;|&nbsp; <a href="documentation.html">One Page</a>
	</div>



<div class="topnavsectionlink">

<a href="index.html">Table of Contents</a>

<div class="prevnext">
Previous: <a href="configuration.html">Configuration</a>

&nbsp; | &nbsp;


Next: <a href="embedding.html">Embedding Python in HTML</a>

</div>
</div>


<div class="topnavmain">
	<div class="topnavheader">Programmatic Configuration</div>
	<div class="topnavitems">
	


    
    
    <div class="toclink">
        <A style="" href="#configuration_programmatic_httphandler">Chaining to HTTPHandler</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#configuration_programmatic_httphandler_ApacheHandler">ApacheHandler</a>
    </div>

    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#configuration_programmatic_httphandler_CGIHandler">CGIHandler</a>
    </div>

    </div>


        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#configuration_programmatic_httphandler_WSGIHandler">WSGIHandler</a>
    </div>

    </div>


    </div>


    
    
    <div class="toclink">
        <A style="" href="#configuration_programmatic_interpreter">Chaining to Interpreter</a>
    </div>
    
    <div class="toclinkcontainer">
        
        
    <div class="toclinkcontainer">
    
    <div class="smalltoclink">
    <A href="#configuration_programmatic_interpreter_standalone">Configuring a Standalone Application</a>
    </div>

    </div>


    </div>



	</div>
</div>

</div>

		<div class="sectioncontent">
			




<div class="subsection" style="margin-left:10px;">

    <div class="sectiontext">


<p>This section illustrates how to link the Myghty interpreter to a Python application.  If you already have a basic configuration running and want to jump into programming templates, skip ahead to <a href="embedding.html#embedding" >Embedding Python in HTML</a>.</p>

<p>The central request-handling unit within Myghty is called the <b>Interpreter</b>.  When an application is set up to handle a request and pass control onto the Interpreter, it is referred to as an <b>external controller</b>.  In the model-view-controller paradigm, the application is the controller, the template called by the Interpreter is the view, and whatever data is passed from the controller to the template is the model.</p>

<p>Currently, all Myghty installations, except for mod_python, require at least a rudimentary external controller, which  serves as the place for configuration directives to be set up and specified to the Myghty environment.  Since a lot of configuration directives reference datastructures or functions themselves, a Python module is the most natural place for this to happen.</p>

<p>A more elaborate external controller may be the point at which an application passes control from its application code and business logic onto a view layer, served by a Myghty Interpreter and corresponding template files.  This may be appropriate for an application that is already handling the details of its HTTP environment, if any, or any application that just needs basic template support.</p>

<p>In contrast to the "external controller" concept is the <b>internal controller</b>, which is Python code that executes after the Interpreter begins handling the request, forwarding data onto template-driven components at the end of its request cycle.  Myghty provides a construct for this kind of controller described in <a href="modulecomponents.html#modulecomponents" >Module Components</a>.  It is recommended that an application that aims to be designed in a full MVC (model-view-controller) style, particularly one that runs in an HTTP environment, place its controller code into Module Components, and only application configuration code and externally-dependent code into a single external controller module.  That way the majority of an application's controller code is written in an environment-neutral fashion, and also has the full benefits of the Myghty resolution and request environment available to it.</p>

<p>The two general categories of external controller are:</p>
<ul>
	<li><a href="programmatic.html#configuration_programmatic_httphandler" >Chaining to HTTPHandler</a></li>
	<li><a href="programmatic.html#configuration_programmatic_interpreter" >Chaining to Interpreter</a></li>
</ul>


			



			



    </div>

        



<A name="configuration_programmatic_httphandler"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Chaining to HTTPHandler</span>
    <div class="sectiontext">


<p>For applications running in an HTTP environment, chaining to HTTPHandler is the usual method.  An HTTPHandler object has awareness of HTTP requests for any of four different environments, which are WSGI, CGI, mod_python, and the standalone handler.  It can construct the appropriate HTTP-aware environment to be delivered to the Interpreter and ultimately onto templates, which then receive an implementation-neutral interface to that environment via the <i>r</i> global object.</p>

<p>All HTTP-based Myghty configurations utilize a subclass of <span class="codeline">myghty.http.HTTPHandler.HTTPHandler</span> to serve requests.  Each HTTP environment has its own module: <span class="codeline">myghty.http.ApacheHandler</span>, <span class="codeline">myghty.http.CGIHandler</span>, <span class="codeline">myghty.http.HTTPServerHandler</span> and <span class="codeline">myghty.http.WSGIHandler</span>, containing the classes <span class="codeline">ApacheHandler</span>, <span class="codeline">CGIHandler</span>, <span class="codeline">HSHandler</span>, and <span class="codeline">WSGIHandler</span>, respectively.</p>

<p>As of version 0.98, the recommended way to chain to an HTTPHandler is to first get an instance to a handler via the function <span class="codeline">get_handler()</span>, and then execute a request on that handler via the method <span class="codeline">handle()</span>.   In previous versions, a single function <span class="codeline">handle()</span> is used which combines the argument sets of both functions; this function is still available.</p>

<p>The get_handler function retrieves a handler from a registry based on the given <b>interpreter_name</b>, which defaults to 'main' or in the case of Apache uses the http.conf variable "MyghtyInterpreterName".  Application-scoped configuration variables are sent to this method which are used to create the initial HTTPHandler object.  Once created, subsequent calls with the same interpreter_name will return the same HTTPHandler instance.
</p>

<p>HTTPHandler then supplies the method <span class="codeline">handle()</span> to handle requests, which accepts configurational parameters on a per request basis.  Common per-request parameters include the component path or object to serve, the request arguments, and out_buffer to capture component output.</p>

<p>Each handler module has a slightly different calling signature, illustrated below.  </p>


			


			


			


    </div>

        



<A name="configuration_programmatic_httphandler_ApacheHandler"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">ApacheHandler</span>
    <div class="sectiontext">


<p>The ApacheHandler handles mod_python requests.  It retrieves configuration via directives found in the host's httpd.conf file in the manner detailed in <a href="configuration.html#configuration_mod_python" >mod_python</a>, and by default serves the component referenced by the <span class="codeline">request.uri</span> data member.  Configuration parameters sent programmatically override those found in the Apache configuration.</p>

<p>In the example below, a file "myghty_handler.py" is created, which contains a very simple mod_python handler that "chains" to the myghty ApacheHandler.</p>



<div class="code">
    <div class="codetitle">myghty_handler.py</div>
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">http</span><span class="python_operator">.</span><span class="python_name">ApacheHandler </span><span class="python_keyword">as </span><span class="python_name">ApacheHandler</span><span class="python_operator">
</span><span class="python_keyword">from </span><span class="python_name">mod_python </span><span class="python_keyword">import </span><span class="python_name">apache</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">handler </span><span class="python_operator">= </span><span class="python_name">ApacheHandler</span><span class="python_operator">.</span><span class="python_name">get_handler</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_keyword">return </span><span class="python_name">handler</span><span class="python_operator">.</span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>A configuration for the above file is similar to a straight Apache configuration.  Since the ApacheHandler created in the myghty_handler.py file contains no configuration at all, all of its options will come from the httpd.conf directives:</p>

    

<div class="code">
<pre>AddHandler mod_python .myt
PythonHandler myghty_handler::handle
PythonPath "sys.path+[r'/path/to/my/libraries']"
PythonOption MyghtyComponentRoot r"/path/to/htdocs"
PythonOption MyghtyDataDir r"/path/to/writeable/data/directory/"</pre></div>


<p>When we take the above handler file and add configuration directives programmatically, they will override those named in the httpd.conf file:</p>



<div class="code">
    <div class="codetitle">myghty_handler.py</div>
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">http</span><span class="python_operator">.</span><span class="python_name">ApacheHandler </span><span class="python_keyword">as </span><span class="python_name">ApacheHandler</span><span class="python_operator">
</span><span class="python_keyword">from </span><span class="python_name">mod_python </span><span class="python_keyword">import </span><span class="python_name">apache</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">handler </span><span class="python_operator">= </span><span class="python_name">ApacheHandler</span><span class="python_operator">.</span><span class="python_name">get_handler</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_operator">,</span>
            <span class="python_name">data_dir</span><span class="python_operator">=</span><span class="python_literal">'/usr/local/web/data'</span><span class="python_operator">,</span>
            <span class="python_name">component_root</span><span class="python_operator">=</span><span class="python_enclosure">[</span>
                <span class="python_enclosure">{</span><span class="python_literal">'components'</span><span class="python_operator">:</span><span class="python_literal">'/usr/local/web/components'</span><span class="python_enclosure">}</span><span class="python_operator">,</span>
                <span class="python_enclosure">{</span><span class="python_literal">'templates'</span><span class="python_operator">:</span><span class="python_literal">'/usr/local/web/templates'</span><span class="python_enclosure">}</span>
            <span class="python_enclosure">])</span><span class="python_operator">
    </span><span class="python_keyword">return </span><span class="python_name">handler</span><span class="python_operator">.</span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<P>Another example, overriding the application's data directory, and also the request's component path and request arguments:</p>


<div class="code">
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">http</span><span class="python_operator">.</span><span class="python_name">ApacheHandler </span><span class="python_keyword">as </span><span class="python_name">ApacheHandler</span><span class="python_operator">
</span><span class="python_keyword">from </span><span class="python_name">mod_python </span><span class="python_keyword">import </span><span class="python_name">apache</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">handler </span><span class="python_operator">= </span><span class="python_name">ApacheHandler</span><span class="python_operator">.</span><span class="python_name">get_handler</span><span class="python_enclosure">(</span>
        <span class="python_name">request</span><span class="python_operator">, </span><span class="python_name">interpreter_name </span><span class="python_operator">= </span><span class="python_literal">'mainhandler'</span><span class="python_operator">, </span><span class="python_name">data_dir </span><span class="python_operator">= </span><span class="python_literal">'/data'</span>
    <span class="python_enclosure">)</span><span class="python_operator">
    </span><span class="python_keyword">return </span><span class="python_name">handler</span><span class="python_operator">.</span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_operator">, </span>
        <span class="python_name">component </span><span class="python_operator">= </span><span class="python_literal">'mypage.myt'</span><span class="python_operator">, </span><span class="python_name">request_args </span><span class="python_operator">= </span><span class="python_enclosure">{</span><span class="python_literal">'foo' </span><span class="python_operator">: </span><span class="python_literal">'bar'</span><span class="python_enclosure">}</span>
    <span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>

<p>The above example also specifies the <a href="params.html#parameters_interpreter_name" >interpreter_name</a> configuration parameter which identifies which ApacheHandler is returned by <span class="codeline">get_handler</span>.  If this parameter is not specified, it defaults to "main".</p>

    </div>


    <a href="#configuration_programmatic" class="toclink">back to section top</a>


</div>


        



<A name="configuration_programmatic_httphandler_CGIHandler"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">CGIHandler</span>
    <div class="sectiontext">


<p>The CGI handler retreives its environment information via the <span class="codeline">cgi.FieldStorage()</span> method as well as <span class="codeline">os.environ</span>.  Configuration parameters sent programmatically override those found in the CGI environment.  By default, CGIHandler serves the component indicated by the environment variable PATH_INFO.
</p>  



<div class="code">
    <div class="codetitle">CGI application chaining to CGIHandler.handle() function</div>
<pre><span class="python_comment">#!/usr/local/bin/python
</span>
<span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">http</span><span class="python_operator">.</span><span class="python_name">CGIHandler </span><span class="python_keyword">as </span><span class="python_name">CGIHandler</span><span class="python_operator">
</span>
<span class="python_comment"># serve the component based on PATH_INFO
</span><span class="python_name">CGIHandler</span><span class="python_operator">.</span><span class="python_name">get_handler</span><span class="python_enclosure">(</span>
        <span class="python_name">component_root</span><span class="python_operator">=</span><span class="python_literal">'/path/to/htdocs'</span><span class="python_operator">,</span>
        <span class="python_name">data_dir</span><span class="python_operator">=</span><span class="python_literal">'/path/to/datadirectory'</span>
<span class="python_enclosure">)</span><span class="python_operator">.</span><span class="python_name">handle</span><span class="python_enclosure">()</span><span class="python_operator"></span></pre></div>




    </div>


    <a href="#configuration_programmatic" class="toclink">back to section top</a>


</div>


        



<A name="configuration_programmatic_httphandler_WSGIHandler"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">WSGIHandler</span>
    <div class="sectiontext">


<p>WSGIHandler works similarly to CGIHandler.  Its <i>r</i> object maintains a reference to both the <span class="codeline">environ</span> and <span class="codeline">start_response</span> members.  These members are used to extract the core data members of the <i>r</i> object, such as <span class="codeline">headers_in</span>, <span class="codeline">method</span>, etc.  </p>

<p>When running under WSGI, the <span class="codeline">environ</span> and <span class="codeline">start_response</span> variables are available via:


<div class="code">
<pre><span class="python_comment"># WSGI environment variables
</span>
<span class="python_name">r</span><span class="python_operator">.</span><span class="python_name">environ</span><span class="python_operator">
</span><span class="python_name">r</span><span class="python_operator">.</span><span class="python_name">start_response</span><span class="python_operator"></span></pre></div>
	
</p> 



<div class="code">
    <div class="codetitle">WSGI application chaining to WSGIHandler.handle() method</div>
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">http</span><span class="python_operator">.</span><span class="python_name">WSGIHandler </span><span class="python_keyword">as </span><span class="python_name">WSGIHandler</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">application</span><span class="python_enclosure">(</span><span class="python_name">environ</span><span class="python_operator">, </span><span class="python_name">start_response</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_keyword">return </span><span class="python_name">WSGIHandler</span><span class="python_operator">.</span><span class="python_name">get_handler</span><span class="python_enclosure">(</span>
        <span class="python_name">component_root</span><span class="python_operator">=</span><span class="python_literal">'/path/to/htdocs'</span><span class="python_operator">, </span>
        <span class="python_name">data_dir</span><span class="python_operator">=</span><span class="python_literal">'/path/to/datadirectory'</span><span class="python_enclosure">)</span><span class="python_operator">.</span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">environ</span><span class="python_operator">, </span><span class="python_name">start_response</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>Also supported with WSGI is the standard <span class="codeline">application(environ, start_response)</span> function, which takes in all application and request-scoped configuration parameters via the <span class="codeline">environ</span> argument:</p>


<div class="code">
    <div class="codetitle">WSGI application via application()</div>
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">http</span><span class="python_operator">.</span><span class="python_name">WSGIHandler </span><span class="python_keyword">as </span><span class="python_name">WSGIHandler</span><span class="python_operator">
</span>
<span class="python_name">params </span><span class="python_operator">= </span><span class="python_name">dict</span><span class="python_enclosure">(</span>
    <span class="python_name">interpreter_name</span><span class="python_operator">=</span><span class="python_literal">'main_wsgi'</span><span class="python_operator">,</span>
    <span class="python_name">component_root</span><span class="python_operator">=</span><span class="python_literal">'/path/to/htdocs'</span><span class="python_operator">, </span>
    <span class="python_name">data_dir</span><span class="python_operator">=</span><span class="python_literal">'/path/to/datadirectory'</span>
<span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">application</span><span class="python_enclosure">(</span><span class="python_name">environ</span><span class="python_operator">, </span><span class="python_name">start_response</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_name">environ</span><span class="python_enclosure">[</span><span class="python_literal">'myghty.application'</span><span class="python_enclosure">] </span><span class="python_operator">= </span><span class="python_name">params</span><span class="python_operator">
    </span><span class="python_name">environ</span><span class="python_enclosure">[</span><span class="python_literal">'myghty.request'</span><span class="python_enclosure">] </span><span class="python_operator">= </span><span class="python_enclosure">{</span><span class="python_literal">'component' </span><span class="python_operator">: </span><span class="python_literal">'mycomponent.myt'</span><span class="python_enclosure">}</span><span class="python_operator">
    </span><span class="python_keyword">return </span><span class="python_name">WSGIHandler</span><span class="python_operator">.</span><span class="python_name">application</span><span class="python_enclosure">(</span><span class="python_name">environ</span><span class="python_operator">, </span><span class="python_name">start_response</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>




    </div>


    <a href="#configuration_programmatic" class="toclink">back to section top</a>


</div>





</div>


        



<A name="configuration_programmatic_interpreter"></a>

<div class="subsection" style="margin-left:20px;">

    <span class="sectionheadertext">Chaining to Interpreter</span>
    <div class="sectiontext">



<p>The Myghty Interpreter object is the underlying engine that creates Myghty requests and executes them, supplying the necessary services each request needs.   The Interpreter can be programmatically instantiated with a full set of configuration parameters and used directly.</p>
<p>While the advantage of using an HTTPHandler in an application is that Myghty components are aware of HTTP-specific information, such as the mod_python request object, HTTP headers, the httpd.conf configuration, etc., an application can also bypass all this by chaining directly to the Interpreter, if templates do not need HTTP awareness and the configuration can be programatically specified.</p>



<div class="code">
    <div class="codetitle">mod_python handler chaining to Interpreter</div>
<pre><span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">interp </span><span class="python_keyword">as </span><span class="python_name">interp</span><span class="python_operator">
</span><span class="python_keyword">from </span><span class="python_name">mod_python </span><span class="python_keyword">import </span><span class="python_name">apache</span><span class="python_operator">
</span>
<span class="python_comment"># set up a singleton instance of Interpreter
</span><span class="python_name">interpreter </span><span class="python_operator">= </span><span class="python_name">interp</span><span class="python_operator">.</span><span class="python_name">Interpreter</span><span class="python_enclosure">(</span>
        <span class="python_name">data_dir </span><span class="python_operator">= </span><span class="python_literal">'./cache'</span><span class="python_operator">,</span>
        <span class="python_name">component_root </span><span class="python_operator">= </span><span class="python_literal">'./doc/components'</span><span class="python_operator">,</span>
    <span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_keyword">def </span><span class="python_name">handle</span><span class="python_enclosure">(</span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator">:
    </span><span class="python_comment"># set up some data to send to the template
</span><span class="python_operator">    </span><span class="python_name">data </span><span class="python_operator">= </span><span class="python_enclosure">{</span>
        <span class="python_literal">'foo'</span><span class="python_operator">: </span><span class="python_literal">'bar'</span><span class="python_operator">,</span>
        <span class="python_literal">'info' </span><span class="python_operator">: </span><span class="python_name">get_info</span><span class="python_enclosure">()</span>
    <span class="python_enclosure">}</span><span class="python_operator">
</span>
    <span class="python_comment"># call a template
    </span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">execute</span><span class="python_enclosure">(</span><span class="python_literal">'mytemplate.myt'</span><span class="python_operator">, </span><span class="python_name">request_args </span><span class="python_operator">= </span><span class="python_name">data</span><span class="python_operator">, </span><span class="python_name">out_buffer </span><span class="python_operator">= </span><span class="python_name">request</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>In the above approach, Myghty components are unaware of the HTTP environment, meaning there is no <i>r</i> global variable, and also can't make HTTP return calls or location redirects.  A greater amount of responsibility is placed within the controller.</p>


				


    </div>

        



<A name="configuration_programmatic_interpreter_standalone"></a>

<div class="subsection" style="margin-left:30px;">

    <span class="sectionheadertext">Configuring a Standalone Application</span>
    <div class="sectiontext">


<p>Chaining to the Interpreter directly also allows Myghty to run outside of a web context completely, within any Python application.  It doesnt really need much configuration to run in "barebones" mode; the two most common features are the component root, which specifies one or more roots to search for input files, and the optional data directory, where it will store compiled python modules.  Without the data directory, the compiled python modules are created in memory only.</p>
<p>A standlone application to run Myghty templates looks like this:</p>



<div class="code">
<pre><span class="python_comment">#!/usr/local/bin/python
</span>
<span class="python_keyword">import </span><span class="python_name">myghty</span><span class="python_operator">.</span><span class="python_name">interp </span><span class="python_keyword">as </span><span class="python_name">interp</span><span class="python_operator">
</span><span class="python_keyword">import </span><span class="python_name">sys</span><span class="python_operator">
</span>
<span class="python_name">interpreter </span><span class="python_operator">= </span><span class="python_name">interp</span><span class="python_operator">.</span><span class="python_name">Interpreter</span><span class="python_enclosure">(</span>
        <span class="python_name">data_dir </span><span class="python_operator">= </span><span class="python_literal">'./cache'</span><span class="python_operator">,</span>
        <span class="python_name">component_root </span><span class="python_operator">= </span><span class="python_literal">'./doc/components'</span><span class="python_operator">,</span>
        <span class="python_name">out_buffer </span><span class="python_operator">= </span><span class="python_name">sys</span><span class="python_operator">.</span><span class="python_name">stdout</span>
    <span class="python_enclosure">)</span><span class="python_operator">
</span>
<span class="python_comment"># component name is relative to component_root
</span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">execute</span><span class="python_enclosure">(</span><span class="python_literal">'/index.myt'</span><span class="python_enclosure">)</span><span class="python_operator"></span></pre></div>


<p>The <span class="codeline">execute</span> method of Intepreter takes optional parameters and sends them off to a newly created <span class="codeline">Request</span> object.  You can specify any of the constructor arguments used by <span class="codeline">Request</span> or its embedded helper object <span class="codeline">RequestImpl</span> in the <span class="codeline">execute</span> call of Interpreter, which will override the values given in the constructor to Interpreter.  The below example sends a buffer to each request with which to capture output, via the <a href="params.html#parameters_out_buffer" >out_buffer</a> parameter:</p>



<div class="code">
<pre><span class="python_name">file </span><span class="python_operator">= </span><span class="python_name">open</span><span class="python_enclosure">(</span><span class="python_literal">'index.html'</span><span class="python_operator">, </span><span class="python_literal">'w'</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="python_name">interpreter</span><span class="python_operator">.</span><span class="python_name">execute</span><span class="python_enclosure">(</span><span class="python_literal">'/index.myt'</span><span class="python_operator">, </span><span class="python_name">out_buffer </span><span class="python_operator">= </span><span class="python_name">file</span><span class="python_enclosure">)</span><span class="python_operator">
</span><span class="python_name">file</span><span class="python_operator">.</span><span class="python_name">close</span><span class="python_enclosure">()</span><span class="python_operator"></span></pre></div>



    </div>


    <a href="#configuration_programmatic" class="toclink">back to section top</a>


</div>





</div>




    
<div class="sectionnavblock">
<div class="sectionnav">


        Previous: <a href="configuration.html">Configuration</a>

                |

        Next: <a href="embedding.html">Embedding Python in HTML</a>

</div>
</div>


</div>


		</div>






</div>

















<center>
</center>
</body>
</html>