Skip to main content

56 posts tagged with "TurboGears"

View All Tags

· One min read

利用 TurboDjango Template Plugin, 可以在 TurboGears, Buffet, 或 CherryPy 裡如此這般調用 Django 模板:

在 controller.py 裡

{{{
@expose(template = "django:dem.templates.djpage")
def page(self):
return dict(name="TurboGears")

<div id="qhide_141663" style="display: block;" class="qt">}}}

用 "template" 參數指定樣板時加一個 "django:" 前綴, 或是在設定檔 app.cfg 中將預設的 tg.defaultview = "kid" 改成 tg.defaultview = "django" 就可以在全站使用 django 模板.

djpage 是模板名稱,return dict 後面給的參數群是要傳給模板的參數.

在 djpage.html 裡


[h1]Hello, {{name}}[/h1]
<div id="qhide_141664" style="display: block;" class="qt">}}}

djpage 是模板名稱,html 是檔名

網頁 http://localhost:8080/page 的輸出結果是

"

Hello, {{name}}

"

· 2 min read

Continue to my previous article, here is a configuration using eclipse external tools to start web server without open start-proj.py

Here is a list of the current functionality:

External Tools:

  • Start Server

  • Start ToolboxAccessibility:

  • Auto Completion Configure Eclipse to Launch developer server

  • Select Run -> External Tools -> External Tools

  • Select "Program" from the configuration tree Select New to create a new program Enter “Start Server” as the name Enter C:\Python24\pythonw.exe for the Location For the Working Directory, use ${project_loc}

  • Enter start-${project_name}.py for the Arguments

  • Select the refresh tab and check Refresh Resources Upon Completion

  • Select the Common tab

        *   Find the section for **Display in Favorites Menu**
    * Check **External Tools **option**

    * Select Apply** Configure Eclipse to Launch ToolBox

  • Select Run -> External Tools -> External Tools

  • Select "Program" from the configuration tree Select New to create a new program Enter “Start ToolBox” as the name Enter C:\Python24\Scripts\tg-admin.exe for the Location For the Working Directory, use ${project_loc}

  • Enter toolbox** for the Arguments**

  • Select the Common tab

        *   Find the section for **Display in Favorites Menu**
    * Check **External Tools **option**

    * Select Apply**

Auto-completion

  • Select your project in Navigator panel
  • Select Properties on Right click menu.
  • Select pyDev - PYTHONPATH Select Add source folder to add project path in Project Source Folder. it will bring project code-completion and code-analysis.
  • Select Add source folder to add turbogears path in External Source Folder.

it will bring turbogears code-completion PS: I get idea from the rails article, but for mine, you'd have eclipse & pydev installed, I recommand you try EasyEclipse for Python.

· 2 min read

NOTICE: This Article is updated for 0.9w3

I recently update my TurboGears PlotKit widget. This version(0.9w3, 0.9 is origin lib's version, w means 'widget')

To get these widgets, easy_install them :

$easy_install plotkit

Then, you can Check the demo in TurboGears toolbox :D

How to try it? Well, it's extremely simple.

For a fresh quickstart site,

In controllers.py:

from plotkit import EasyPlot

class Root(controllers.RootController):
@expose(template="wgtest.templates.welcome")
def index(self):
setA = [[0,0], [1,2], [2,3], [3,7], [4,8], [5,6]]
setB = [[0,0], [1,1], [2,4], [3,8], [4,7], [5,20]]
setC = [[0,1], [1,3], [2,5], [3,5], [4,3], [5,2]]
return dict(ep= EasyPlot(id="diag",
style="line",
width="300",
height="300",
data=[setA, setB, setC]))

In welcome.kid:

${ep.display()}

add this script into html "body".

Explain:

from plotkit import EasyPlot

First of all, import the proper widget. (origin PlotKit widget is still availbe)

return dict(ep= EasyPlot(...))

return the "ep" diagram instance to template

${ep.display()}

display the diagram in template

Parameters:

id: (diag) The Diagram's Identifer (Element ID)

style: (line) The diagram could be plot as a "line", "pie", or "bar"diagram.

width, height : (400, 400)

The diagram's size

data : Datasets is structured with [[x,y],[x1,y1],....]

option : options of both Layout and Renderer (dictionary style), need be a string

The origin example is here.


History 0.9w3 07/04/2006: more flexible EasyPlot dataset 0.9w2 : fix bugs and add a EasyPlot() widget to plot a diagram within the code.

The svn repository is in Sourceforge, you can share your TurboGears widget there, too.

known limit: current widget's option param suppor is not very OO, you need pass the dictionary in string format (can't recognize "v", "label")

· One min read

習慣用微軟 VisualStudio 或用 Java 的人用了 python 語言, 通常會挑剔 python 語言沒什麼好用的 IDE 來搭配.

現在 EasyEclipse 有提供包好的 eclipse + pydev (eclipse 平台上的 python 語言支援 -) + Subclipse (eclipse 平台上的圖形化 SVN 版本控制工具) http://www.easyeclipse.org/site/distributions/python.html

簡化了 eclipse 平台安裝設定上的繁瑣,拿來做 python 程式開發其實還蠻不賴的,

· 3 min read

補充一點心得:

如果你的網頁應用服務主要關注在 AJAX 應用,大部分動作都用 javascript 在客戶端完成,

只有 data 部分需要後端提供。那麼 TurboGears 是非常適用的選擇.

1. 可以先用一般 serverside 開發方式寫函式和建立網頁應用服務原型 (prototype),

來測試你的網頁應用服務該有的功能.

@expose (format = ".template.pages") #資料以樣版格式顯示 def method (self): .... return dict {data=data} 因為 TurboGears 中從傳入 serverside 的表單資料處理一致,

所以在 serverside 寫的 code 完全可以繼續使用,

不必為了支持 AJAX 重寫,很好的達到不重複自己 (DRY) 的效果.

2.import javascript library , 將資料改以 JSON 格式傳到網頁

from turbogears import mochikit ... @expose (format = ".template.pages") #資料以樣版格式顯示 @expose (format = "JSON") #資料以 JSON 格式顯示 def method (self): .... return dict {data=data, scripty = mochikit} #在網頁上

TurboGears 預先包好 mochikit, scriptaculous, plotkit 等 javascript 庫,

使用時可以用程式呼叫,預設可用 JSON 格式傳輸,

預設 mochikit 庫提供相應資料處理支援.

3. 在 client 端用 javascript 處理 DOM 物件.

因為在開發的第一步時已經能將所需的資料,傳輸內容等都處理好了,

能確信資料傳輸的正確性。所以開發 javascript 時,可以專注在網頁內資料處理的部分.

在這時遇到 bug 的話也可以很放心地將可能的問題點縮小到單純網頁內資料處理的範圍,

因而 AJAX 開發時最麻煩的交叉 debug 也變得更容易.

因此如果你的網頁應用服務主要關注在 AJAX 應用,那麼 TurboGears 是非常適合的選擇.