Saturday, November 28, 2009

Generating SVG Output (from Graphviz) in your Django App

So accomplishing new coding tasks can be a challenge with interruptions and I've had a lot of interruptions this week but I finally got there. And I'm thankful!

I have an app that is storing data, meaning Django models for the uninitiated. What is in there doesn't matter, but it is something that is conducive to plotting with graphviz. So the starting point is a string that is in the .dot format. I have some code that makes queries to the database and I end up with a string.

So there is a utility function that creates this string...

def make_svg_str:
#blah blah blah snip
dot_string += "}"
p = subprocess.Popen('/usr/bin/dot -Tsvg', shell=True,\
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout,stderr) = p.communicate(dot_string)
return stdout

So I almost got this right the first time except that I forgot the stdout in Popen() which caused the output to go to stdout (and not be assigned to the string) so I saw the .xml in the dev web server logs.

The graphviz string (dot_string) is being piped to the dot executable and then the function is returning the XML SVG as a string, and is obviously assigned to the stdout variable in the tuple.

Now the tricky part within my views.py.

My first mistake was using the Django CSV docs instead of the PDF docs because the latter is what we need. I also didn't remember that HttpResponse is a file-like object so we can can just write to it once we have the SVG text.

def svg(request):
f = Foo.objects.all()
response = HttpResponse(mimetype='image/svg+xml')
response['Content-Disposition'] = 'filename=somefilename.svg'
response.write(make_svg_str(f))
return response

So this will display your image within your browser (which is what I wanted) instead of downloading file if you the use the "attachment" in the Content-Disposition key.

The name of the game is taking shortcuts that get the job done. I'm using the admin interface to provide a good-enough UI to enter the data and now I'm using Graphviz to visualize that data without having to spend a lot of time writing UIs or nasty JavaScript.

40 comments:

coco said...

情色卡通影片免費觀看情色卡通影片區情色卡提諾情色卡漫情色卡漫貼圖情色口愛辣妹情色另類貼圖情色台灣情色台灣3p情色台灣偷拍短片情色合成圖情色合成圖片情色哈拉情色商品情色圖貼區情色小說,色情小說情色小說.情色小說aa情色影片網情色影片線上直播情色影片線上看情色影片線上觀看情色影片線上觀賞情色影片線上試看情色影片試看情色影片論壇情色影片貼片分享區情色影片免費a片情色影片免費線上看情色影片免費觀看情色影片免費觀賞情色影片免費試看情色影片免費欣賞情色影片免費下載情色影片直播下載網情色影片情色影情色小說網情色小說阿賓情色小說論譠

芥末 said...

謝謝分享好文章.........................

chat said...

蒐狗牙牙健康自療法
寵物狗床

明宏 said...

a成人短片尼克成人電影免費觀看成人動畫成人鴛鴦吧歐美成人區性愛自慰高潮影片性愛色情片性愛色情影片性愛免費短片性愛刺激性愛呻吟性愛姿勢大全性愛昭式性愛敏感地帶性愛短片免費看性愛圖網東洋無碼動畫下載區亞洲線上免費杜雷斯成人貼圖杜蕾斯貼片區杜蕾斯貼圖片區杜蕾斯貼圖網杜蕾斯漫畫杜蕾斯影片分享杜蕾斯線上片杉本彩寫真集杉原杏璃影片男人貼圖站男女免費線上影片男女性交做愛影片成人文學網a18禁女生遊戲區熊貓貼圖jp成人

侯淑真 said...

If you can not be kind, at least have the decency to be vague.............................................

v奎峰奎峰 said...

how do u do?xvideo打飛機專用網洪爺免費洪爺色情片洪爺貼圖區洪爺成人線上洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼援交妹辣妹野球拳情色文學情趣聊天室性感辣妹裸體遊戲做愛偷拍一夜情視訊洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺免費洪爺色情片洪爺貼圖區洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼洪爺成人線上

薇兒 said...

淫亂淫婦淫妹淫叫淫水淫女情慾情色做愛限制級波霸口交18禁貼圖寫真視訊援交露點爆乳潮吹裸體裸照裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情

RexTemples20144 said...

人必須心懷希望,才會活的快樂,日子才過得充實,有意義,有朝氣,有信心。........................................

GeraldF_Rotter雅慧 said...

18禁,聊天,免費影片,美女短片,0401,免費聊天,免費視訊,0204,78論壇,亞洲影城,視訊聊天,免費影片,聊天室,ut,成人,視訊,都都,視訊,打飛機專用網,小高聊天室,視訊,免費影片,a片免費,a片,18,免費線上視訊,日本a電影,影音視訊聊天室,貼圖區,av,383,77p2p,85cc,無碼,av影片,av專賣店,a片線上看,a圖,免費線上影片,微風成人

上心 said...

裸體寫真全裸美女圖片色情訊息黃色圖片自拍裸體圖片sex裸露圖片18限85cc a片台灣色情網站免費色情圖一夜激情聊天情色聊天室限制級爆乳女優作愛巨乳學院性愛情慾陰脣一夜情下體網愛聊天鹹濕做愛自拍成人圖庫成人影城性關係視訊情人性影片觀賞裸照淫美成人論壇av寫真自拍裸女貼圖av圖情色性愛貼圖成人vcdsexy辣妹視訊聊天色情視訊淫婦台灣情色論壇丁字褲貼圖免費a片影片淫蕩女人live show男女做愛火辣妹妹激情網愛聊天美女裸照免費色情網站

嘉玲嘉玲 said...

認清問題就等於已經解決了一半的問題。.............................................

韋于倫成 said...

河水永遠是相同的,可是每一剎那又都是新的。........................

HaroldM22 said...

777成人貼圖區 激情視訊 1069聊天 免費線上a 一葉情交友 av免費看女優 性感卡通美女圖片 成人線上影片觀賞 熟女自拍偷拍 dd 豆豆聊聊天室 性愛愛無碼影片 情趣成人圖片 18禁網站 少女卡通圖 777線上影城 忍者a片 咪咪情色貼圖 色情免費下載影片 一夜情網路交友 a383girl影音 完美女聊天 玩美影音 線上美女 辣妹脫衣 人妻自拍分享 熟女,貼圖 護士 空姐 a圖貼片區 日本女郎寫真集 85cc線上免費影片 微風成人 嘟嘟 777中部人聊天室 性貼圖區 學生性愛援交 情人視訊辣妹 鋼管 免費av線上觀看 洪爺情色電影 大奶. sex無碼影片 免下載色情網站 日本正妹巨乳照片 愛情性遊戲區 後宮電影電影院入口 限制級 成人視訊0204 女人性愛貼片 乳頭走光

怡谷ShanikaMaxey怡谷 said...

我們必須先有哭泣,才有歡笑;也必須先感到人生的悲哀,然後才感到人生的快樂。 ..................................................

佩GailBohanan1蓉 said...

你不能決定生命的長度,但你可以控制它的寬度............................................................

burtong said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。...............................................................

國昆國昆 said...

It is no use crying over spilt milk..................................................................

玫友 said...

光這幾句話就價值連城了,讀著讀著小弟的眼眶就濕了... .................................................................

俊源 said...

死亡是悲哀的,但活得不快樂更悲哀。.................................................................                           

文辰文辰 said...

一定要保持最佳狀況呦,加油!!!期待你發表的新文章!....................................................................

珍昕珍昕 said...

成熟,就是有能力適應生活中的模糊。.................................................................

李李淑博淑林嘉宏真 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

原秋原秋 said...

你的分享很不錯.. 謝謝 ..................................................................

偉軍 said...

一個人就像一個分數,他的實際才能是分子,他對自己的評價是分母。分母越大,則分數的價值越小。..................................................

吳婷婷 said...

People throw stones only at trees with fruit on them.............................................................

倫惟倫惟 said...

Birth is much but breeding is more. 加油!..................................................................

江桂宸江桂宸 said...

此時此刻~祝福您越來越好~健康開心..................................................................

宮惠如宮惠如 said...

來給你加油打氣!!............................................................

楊儀卉 said...

我喜歡用心經營的blog~ ^^..................................................................

謝俊柯凡豐德 said...

Pen and ink is wits plough. ............................................................

潘兆以潘兆以 said...

打擾你實在不好意思, 你的blog很好看呢!!!!............................................................

黃書豪黃書豪黃書豪 said...

人應該做自己認為對的事,而不是一味跟著群眾的建議走。..................................................

文王廷 said...

Learning makes a good man better and ill man worse.............................................................

家唐銘 said...

Seeing is believing.百聞不如一見............................................................

鄭雅雯 said...

我們能互相給予的最佳禮物是「真心的關懷」。.................................................

偉曹琬 said...

Poverty tries friends...................................................................

江仁趙雲虹昆 said...

若有人問你成功時會不會記得他 試問若你失敗時他會不會記得你......................................................................

惠桂刁惠桂刁惠桂刁 said...

卡爾.桑得柏:「除非先有夢,否則一切皆不成。」共勉!.. .. ............................................................

淑蕙春蕙春芬 said...

Joy often comes after sorrow, like morning after night.. . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

翊翊翊翊張瑜翊翊翊 said...

初到貴寶地,拜碼頭!!!請多指教!..................................................................