Skip to main content

97 posts tagged with "python"

View All Tags

· One min read

我想,這是第一次有了個網站框架將它的功能做得這麼容易明白. I think this is the first time that a web framework's functionality has been so obvious -- Bruce Eckel

原文: The Web Frameworks Jam and Turbogears

誰是 Bruce Eckel ?

Bruce Eckel 是 Thinking in Java, Thinking in C++ 系列書的作者,作品享有很高的聲譽.

· 2 min read

Lots of people use Django template for Pyhton Web Framework(As Django project shares).

And if we want to make people feel comfortable to try TurboGears (and then migrate to tg hopefully), first thing we can do is making django template system available on TurboGears.

Its easy because many template plugins (and their source codes) are available for TurboGears, I use a couple hours to read the template plugins doc and make the TurboDjango template plugin.

You can try the django template in minutes. Here is the step by step guide:

Install By SetupTools

$ easy_install TurboDjango

How to Make it just Work?

Step 1. Quickstart

Use tg-admin tool to create a project

$ tg-admin quickstart tgdj

The project name is "tgdj"

Step 2. Edit Controller

Edit tgdj/tgdj/controller.py, add following method in "Root" class.

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

Step 3. Edit Template

Edit tgdj/tgdj/templates/djpage.html

[h1]Hello, {{name}}[/h1]

The template has "html" file suffix

Step 4. Check Result

In tgdj, start develop server by typing

$ start-tgdj

Then surf http://localhost:8080/page, you'll see

Hello, TurboGears

The Release is available here.

To Django Users: You can migrate your database with SQLAlchemy's SqlSoup

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