読み書きプログラミング

日常のプログラミングで気づいたことを綴っています

Maxima on Web グラフ出力

maxima.html

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Maxima on Web</title>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript"><!--
function execute() {
  if ($('command').value.length > 0) {
    var xmlHttp = new Ajax.Request(
      'cgi-bin/maxima.rb?',
      {method: 'post',
      postBody: $('command').value,
      onSuccess: function(request) {
        $('result').innerHTML = request.responseText;
        },
      onFailure: function(request) {
        alert('failure');
      },
      onException: function(request) {
        alert('exception');
      }
    });
  }
}
// --></script>
</head>
<body>
<h1>Maxima on Web</h1>
<form>
<textarea id="command" rows="4" cols="80"></textarea>
<input type="button" value="evaluate!" onClick="execute()" />
</form>
<div id="result"></div>
</body>
</html>

HTTPリクエストをGETからPOSTに変更しました。


maxima.rb

#!/usr/bin/ruby
# -*- coding: utf-8 -*-
ENV['PATH'] = '/opt/local/bin:' + ENV['PATH'] # gnuplot用パスを追加
texfile = 'maxima.tex'
graphfile = 'images/maxima.png'
graphrelative = '../' + graphfile
# Maximaのtexコマンドはファイルに追加していくので最初にファイルを消しておく。
`rm #{texfile}` 
`rm #{graphrelative}` 

print "Content-Type: text/plain\n\n"

cmd = $stdin.gets
if /^\s*$/ =~ cmd then
  exit
end
if /[;$]$/ !~ cmd then
  cmd = cmd + '$' #cmdの最後に$や;がなければ$を追加する。
end
cmd = 'set_plot_option([gnuplot_term, png]);\n' \
"set_plot_option([gnuplot_out_file, \\\"#{graphrelative}\\\"]);\n" \
+ cmd + '\ntex(%, \\"' + texfile + '\\");'
`echo "#{cmd}" | /Applications/Maxima.app/Contents/Resources/maxima.sh`

f = open(texfile)
result = f.read
if result.length > '$$$$'.length then #'$$$$'は空っぽの結果
  puts '<div align="center">'
  puts "<img src=\"http://latex.codecogs.com/gif.latex?#{result}\" />"
  puts '</div>'
end
f.close
if File.exist?(graphrelative) then
  puts '<div align="center">'
  puts "<img src=\"#{graphfile}\" />"
  puts '</div>'
end

ウェブサーバーからCGIを起動すると、gnuplotへのパスが見えていなかったのが問題でした。


ようやくスタート地点に立てました。ここからが本番です。iPadの良さを引き出したいです。

  1. Javascriptによる入力支援(タイプじゃなくてドラッグ&ドロップ的なGUI)
  2. Ajaxによるヘルプ
  3. Maximaのセッション対応