Skip to main content

PlotKit with EasyPlot in TurboGears

· 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")

EasyEclipse for Python

· 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 程式開發其實還蠻不賴的,

TurboGears AJAX 使用感想- 2

· 2 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 是非常適合的選擇.