gregsexton / ob-ipython

org-babel integration with Jupyter for evaluation of (Python by default) code blocks
739 stars 109 forks source link

Other solution to get rid of `Trying to parse HTTP response code in odd buffer` #60

Open fanpeng-kong opened 8 years ago

fanpeng-kong commented 8 years ago

I just pulled the recent repo and get the error "Trying to parse HTTP response code in odd buffer". After checked the Issue #22 and 33, I found I can solve the problem with (setq python-shell-interpreter "/usr/local/bin/python"). However, I take the courage to ask: if there is other solution instead of this? And here is the reason:

I use the Elpy mode for my python-mode and I really enjoy that. I set (elpy-use-ipython) to use ipython for Elpy. If I set the python-shell-interpreter to python, the ob-ipython will works fine but broke the Elpy workflow. Interesting thing is I did not have this problem with previous version of ob-ipython.

BTW, thanks @gregsexton for this great package. I used to use EIN before but not anymore since I found ob-ipython, I do prefer the Org-mode more. And if anyone can auto complete with company-mode from different source blocks under the same :session, could you kindly give me some info on your setup, as I still suffer the Issue 54.

Many thanks Fanpeng

Some information might be helpful:

  1. emacs-version: "GNU Emacs 24.5.1 (x86_64-apple-darwin15.3.0, NS apple-appkit-1404.34) of 2016-02-26 on Jupiter"
  2. org-version: "8.3.4"
  3. ipython version: 4.2.0
  4. jupyter version: 4.1.0

Backtrace: Debugger entered--Lisp error: (error "Trying to parse HTTP response code in odd buffer: *http localhost:9988*-340583") signal(error ("Trying to parse HTTP response code in odd buffer: *http localhost:9988*-340583")) error("Trying to parse HTTP response code in odd buffer: %s" " *http localhost:9988*-340583") url-http-parse-response() (>= (url-http-parse-response) 400) (if (>= (url-http-parse-response) 400) (ob-ipython--dump-error (buffer-string)) (goto-char url-http-end-of-headers) (let ((json-array-type (quote list))) (json-read))) (save-current-buffer (set-buffer (url-retrieve-synchronously (format "http://%s:%d/execute/%s" ob-ipython-driver-hostname ob-ipython-driver-port name))) (if (>= (url-http-parse-response) 400) (ob-ipython--dump-error (buffer-string)) (goto-char url-http-end-of-headers) (let ((json-array-type (quote list))) (json-read)))) (let ((url-request-data code) (url-request-method "POST")) (save-current-buffer (set-buffer (url-retrieve-synchronously (format "http://%s:%d/execute/%s" ob-ipython-driver-hostname ob-ipython-driver-port name))) (if (>= (url-http-parse-response) 400) (ob-ipython--dump-error (buffer-string)) (goto-char url-http-end-of-headers) (let ((json-array-type (quote list))) (json-read))))) ob-ipython--execute-request("%matplotlib inline\nimport matplotlib.pyplot as plt\nimport numpy as np" "example") (ob-ipython--eval (ob-ipython--execute-request (org-babel-expand-body:generic (encode-coding-string body (quote utf-8)) params (org-babel-variable-assignments:python params)) (ob-ipython--normalize-session session))) (let ((ret (ob-ipython--eval (ob-ipython--execute-request (org-babel-expand-body:generic (encode-coding-string body (quote utf-8)) params (org-babel-variable-assignments:python params)) (ob-ipython--normalize-session session))))) (if ret (progn (let ((result (cdr (assoc :result ret))) (output (cdr (assoc :output ret)))) (if (eq result-type (quote output)) output (ob-ipython--create-stdout-buffer output) (cond ((and file ...) (ob-ipython--write-base64-string file ...)) ((and file ...) (ob-ipython--write-string-to-file file ...)) (file (error "%s is currently an unsupported file extension." ...)) (t (cdr ...)))))))) (let* ((file (cdr (assoc :file params))) (session (cdr (assoc :session params))) (result-type (cdr (assoc :result-type params)))) (org-babel-ipython-initiate-session session params) (let ((ret (ob-ipython--eval (ob-ipython--execute-request (org-babel-expand-body:generic (encode-coding-string body ...) params (org-babel-variable-assignments:python params)) (ob-ipython--normalize-session session))))) (if ret (progn (let ((result (cdr ...)) (output (cdr ...))) (if (eq result-type (quote output)) output (ob-ipython--create-stdout-buffer output) (cond (... ...) (... ...) (file ...) (t ...)))))))) org-babel-execute:ipython("%matplotlib inline\nimport matplotlib.pyplot as plt\nimport numpy as np" ((:colname-names) (:rowname-names) (:result-params "replace") (:result-type . value) (:comments . "") (:shebang . "") (:cache . "no") (:padline . "") (:noweb . "no") (:tangle . "no") (:exports . "code") (:results . "replace") (:hlines . "no") (:session . "example"))) funcall(org-babel-execute:ipython "%matplotlib inline\nimport matplotlib.pyplot as plt\nimport numpy as np" ((:colname-names) (:rowname-names) (:result-params "replace") (:result-type . value) (:comments . "") (:shebang . "") (:cache . "no") (:padline . "") (:noweb . "no") (:tangle . "no") (:exports . "code") (:results . "replace") (:hlines . "no") (:session . "example"))) (let ((result (funcall cmd body params))) (if (and (eq (cdr (assoc :result-type params)) (quote value)) (or (member "vector" result-params) (member "table" result-params)) (not (listp result))) (list (list result)) result)) (setq result (let ((result (funcall cmd body params))) (if (and (eq (cdr (assoc :result-type params)) (quote value)) (or (member "vector" result-params) (member "table" result-params)) (not (listp result))) (list (list result)) result))) (if (member "none" result-params) (progn (funcall cmd body params) (message "result silenced") (setq result nil)) (setq result (let ((result (funcall cmd body params))) (if (and (eq (cdr (assoc :result-type params)) (quote value)) (or (member "vector" result-params) (member "table" result-params)) (not (listp result))) (list (list result)) result))) (if (cdr (assoc :file params)) (progn (if result (progn (let ((temp-file ...) (temp-buffer ...)) (unwind-protect (prog1 ... ...) (and ... ...))))) (setq result (cdr (assoc :file params))))) (if (cdr (assoc :post params)) (progn (let ((*this* (if (cdr ...) (org-babel-result-to-file ... ...) result))) (setq result (org-babel-ref-resolve (cdr (assoc :post params)))) (if (cdr (assoc :file params)) (progn (setq result-params (remove "file" result-params))))))) (org-babel-insert-result result result-params info new-hash lang)) (let* ((lang (nth 0 info)) (result-params (cdr (assoc :result-params params))) (body (let* ((c (nthcdr 1 info))) (setcar c (if (org-babel-noweb-p params :eval) (org-babel-expand-noweb-references info) (nth 1 info))))) (dir (cdr (assoc :dir params))) (default-directory (or (and dir (file-name-as-directory (expand-file-name dir))) default-directory)) (cmd (intern (concat "org-babel-execute:" lang))) result) (if (fboundp cmd) nil (error "No org-babel-execute function for %s!" lang)) (message "executing %s code block%s..." (capitalize lang) (if (nth 4 info) (format " (%s)" (nth 4 info)) "")) (if (member "none" result-params) (progn (funcall cmd body params) (message "result silenced") (setq result nil)) (setq result (let ((result (funcall cmd body params))) (if (and (eq (cdr ...) (quote value)) (or (member "vector" result-params) (member "table" result-params)) (not (listp result))) (list (list result)) result))) (if (cdr (assoc :file params)) (progn (if result (progn (let (... ...) (unwind-protect ... ...)))) (setq result (cdr (assoc :file params))))) (if (cdr (assoc :post params)) (progn (let ((*this* (if ... ... result))) (setq result (org-babel-ref-resolve (cdr ...))) (if (cdr (assoc :file params)) (progn (setq result-params ...)))))) (org-babel-insert-result result result-params info new-hash lang)) (run-hooks (quote org-babel-after-execute-hook)) result) (cond (cache-current-p (save-excursion (goto-char (org-babel-where-is-src-block-result nil info)) (forward-line) (skip-chars-forward " ") (let ((result (org-babel-read-result))) (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result))) ((org-babel-confirm-evaluate info) (let* ((lang (nth 0 info)) (result-params (cdr (assoc :result-params params))) (body (let* ((c ...)) (setcar c (if ... ... ...)))) (dir (cdr (assoc :dir params))) (default-directory (or (and dir (file-name-as-directory ...)) default-directory)) (cmd (intern (concat "org-babel-execute:" lang))) result) (if (fboundp cmd) nil (error "No org-babel-execute function for %s!" lang)) (message "executing %s code block%s..." (capitalize lang) (if (nth 4 info) (format " (%s)" (nth 4 info)) "")) (if (member "none" result-params) (progn (funcall cmd body params) (message "result silenced") (setq result nil)) (setq result (let ((result ...)) (if (and ... ... ...) (list ...) result))) (if (cdr (assoc :file params)) (progn (if result (progn ...)) (setq result (cdr ...)))) (if (cdr (assoc :post params)) (progn (let (...) (setq result ...) (if ... ...)))) (org-babel-insert-result result result-params info new-hash lang)) (run-hooks (quote org-babel-after-execute-hook)) result))) (let* ((params (nth 2 info)) (cachep (and (not arg) (cdr (assoc :cache params)) (string= "yes" (cdr (assoc :cache params))))) (new-hash (if cachep (progn (org-babel-sha1-hash info)))) (old-hash (if cachep (progn (org-babel-current-result-hash)))) (cache-current-p (and (not arg) new-hash (equal new-hash old-hash)))) (cond (cache-current-p (save-excursion (goto-char (org-babel-where-is-src-block-result nil info)) (forward-line) (skip-chars-forward " ") (let ((result (org-babel-read-result))) (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result))) ((org-babel-confirm-evaluate info) (let* ((lang (nth 0 info)) (result-params (cdr (assoc :result-params params))) (body (let* (...) (setcar c ...))) (dir (cdr (assoc :dir params))) (default-directory (or (and dir ...) default-directory)) (cmd (intern (concat "org-babel-execute:" lang))) result) (if (fboundp cmd) nil (error "No org-babel-execute function for %s!" lang)) (message "executing %s code block%s..." (capitalize lang) (if (nth 4 info) (format " (%s)" (nth 4 info)) "")) (if (member "none" result-params) (progn (funcall cmd body params) (message "result silenced") (setq result nil)) (setq result (let (...) (if ... ... result))) (if (cdr (assoc :file params)) (progn (if result ...) (setq result ...))) (if (cdr (assoc :post params)) (progn (let ... ... ...))) (org-babel-insert-result result result-params info new-hash lang)) (run-hooks (quote org-babel-after-execute-hook)) result)))) (progn (let* ((c (nthcdr 2 info))) (setcar c (org-babel-process-params (car c)))) (let* ((params (nth 2 info)) (cachep (and (not arg) (cdr (assoc :cache params)) (string= "yes" (cdr (assoc :cache params))))) (new-hash (if cachep (progn (org-babel-sha1-hash info)))) (old-hash (if cachep (progn (org-babel-current-result-hash)))) (cache-current-p (and (not arg) new-hash (equal new-hash old-hash)))) (cond (cache-current-p (save-excursion (goto-char (org-babel-where-is-src-block-result nil info)) (forward-line) (skip-chars-forward " ") (let ((result ...)) (message (replace-regexp-in-string "%" "%%" ...)) result))) ((org-babel-confirm-evaluate info) (let* ((lang (nth 0 info)) (result-params (cdr ...)) (body (let* ... ...)) (dir (cdr ...)) (default-directory (or ... default-directory)) (cmd (intern ...)) result) (if (fboundp cmd) nil (error "No org-babel-execute function for %s!" lang)) (message "executing %s code block%s..." (capitalize lang) (if (nth 4 info) (format " (%s)" ...) "")) (if (member "none" result-params) (progn (funcall cmd body params) (message "result silenced") (setq result nil)) (setq result (let ... ...)) (if (cdr ...) (progn ... ...)) (if (cdr ...) (progn ...)) (org-babel-insert-result result result-params info new-hash lang)) (run-hooks (quote org-babel-after-execute-hook)) result))))) (if (org-babel-check-evaluate info) (progn (let* ((c (nthcdr 2 info))) (setcar c (org-babel-process-params (car c)))) (let* ((params (nth 2 info)) (cachep (and (not arg) (cdr (assoc :cache params)) (string= "yes" (cdr ...)))) (new-hash (if cachep (progn (org-babel-sha1-hash info)))) (old-hash (if cachep (progn (org-babel-current-result-hash)))) (cache-current-p (and (not arg) new-hash (equal new-hash old-hash)))) (cond (cache-current-p (save-excursion (goto-char (org-babel-where-is-src-block-result nil info)) (forward-line) (skip-chars-forward " ") (let (...) (message ...) result))) ((org-babel-confirm-evaluate info) (let* ((lang ...) (result-params ...) (body ...) (dir ...) (default-directory ...) (cmd ...) result) (if (fboundp cmd) nil (error "No org-babel-execute function for %s!" lang)) (message "executing %s code block%s..." (capitalize lang) (if ... ... "")) (if (member "none" result-params) (progn ... ... ...) (setq result ...) (if ... ...) (if ... ...) (org-babel-insert-result result result-params info new-hash lang)) (run-hooks (quote org-babel-after-execute-hook)) result)))))) (let* ((org-babel-current-src-block-location (or org-babel-current-src-block-location (nth 5 info) (org-babel-where-is-src-block-head))) (info (if info (copy-tree info) (org-babel-get-src-block-info)))) (let* ((c (nthcdr 2 info))) (setcar c (org-babel-merge-params (car c) params))) (if (org-babel-check-evaluate info) (progn (let* ((c (nthcdr 2 info))) (setcar c (org-babel-process-params (car c)))) (let* ((params (nth 2 info)) (cachep (and (not arg) (cdr ...) (string= "yes" ...))) (new-hash (if cachep (progn ...))) (old-hash (if cachep (progn ...))) (cache-current-p (and (not arg) new-hash (equal new-hash old-hash)))) (cond (cache-current-p (save-excursion (goto-char ...) (forward-line) (skip-chars-forward " ") (let ... ... result))) ((org-babel-confirm-evaluate info) (let* (... ... ... ... ... ... result) (if ... nil ...) (message "executing %s code block%s..." ... ...) (if ... ... ... ... ... ...) (run-hooks ...) result))))))) org-babel-execute-src-block(nil) (progn (org-babel-eval-wipe-error-buffer) (org-babel-execute-src-block current-prefix-arg) t) (if (memq (org-element-type (org-element-context)) (quote (inline-src-block src-block))) (progn (org-babel-eval-wipe-error-buffer) (org-babel-execute-src-block current-prefix-arg) t) nil) org-babel-execute-src-block-maybe() (or (org-babel-execute-src-block-maybe) (org-babel-lob-execute-maybe)) org-babel-execute-maybe() (if org-babel-no-eval-on-ctrl-c-ctrl-c nil (org-babel-execute-maybe)) org-babel-execute-safely-maybe() run-hook-with-args-until-success(org-babel-execute-safely-maybe) (cond ((or (and (boundp (quote org-clock-overlays)) org-clock-overlays) org-occur-highlights) (and (boundp (quote org-clock-overlays)) (org-clock-remove-overlays)) (org-remove-occur-highlights) (message "Temporary highlights/overlays removed from current buffer")) ((and (local-variable-p (quote org-finish-function) (current-buffer)) (fboundp org-finish-function)) (funcall org-finish-function)) ((run-hook-with-args-until-success (quote org-ctrl-c-ctrl-c-hook))) (t (if (save-excursion (beginning-of-line) (looking-at "[ ]*$")) (or (run-hook-with-args-until-success (quote org-ctrl-c-ctrl-c-final-hook)) (user-error "C-c C-c can do nothing useful at this location")) (let* ((context (org-element-context)) (type (org-element-type context))) (cond ((eql type (quote link)) (setq context (org-element-property :parent context)) (setq type (org-element-type context))) ((memql type (quote ...)) (setq context (org-element-lineage context ...)))) (if (eq type (quote paragraph)) (progn (let (...) (if ... ...)))) (cond ((eql type (quote clock)) (org-clock-update-time-maybe)) ((eql type (quote dynamic-block)) (save-excursion (goto-char ...) (org-update-dblock))) ((eql type (quote footnote-definition)) (goto-char (org-element-property :post-affiliated context)) (call-interactively (quote org-footnote-action))) ((eql type (quote footnote-reference)) (call-interactively (quote org-footnote-action))) ((memql type (quote ...)) (save-excursion (goto-char ...) (call-interactively ...))) ((eql type (quote item)) (let* (... ... ... ... ... ...) (org-list-set-checkbox ... struct ...) (org-list-struct-fix-ind struct parents 2) (org-list-struct-fix-item-end struct) (org-list-struct-fix-bul struct prevs) (org-list-struct-fix-ind struct parents) (let ... ... ...))) ((eql type (quote keyword)) (let (... ...) (if ... ...) (let ... ... --rtn)) (message "Local setup has been refreshed")) ((eql type (quote plain-list)) (let* (... ... ... ... ... ...) (cond ... ...) (org-list-write-struct struct ... old-struct) (org-update-checkbox-count-maybe) (save-excursion ... ...))) ((memql type (quote ...)) (call-interactively (quote org-property-action))) ((memql type (quote ...)) (call-interactively (quote org-update-radio-target-regexp))) ((eql type (quote statistics-cookie)) (call-interactively (quote org-update-statistics-cookies))) ((memql type (quote ...)) (if (eq ... ...) (message "%s" ...) (let ... ...))) ((eql type (quote timestamp)) (org-timestamp-change 0 (quote day))) (t (or (run-hook-with-args-until-success ...) (user-error "C-c C-c can do nothing useful at this location")))))))) org-ctrl-c-ctrl-c(nil) call-interactively(org-ctrl-c-ctrl-c nil nil) command-execute(org-ctrl-c-ctrl-c)

kirk86 commented 8 years ago

I also experience the same exact issue once I updated from the repo and I also use the elpy set to ipython.

julienchastang commented 8 years ago

I too can confirm @fanpeng-kong comments (thanks for figuring this out). I wish only to add that you can easily switch the Python interpreter via elpy-config (under the Python group, Python Shell Interpreter Option). This is not ideal, but can at least be done quickly to minimize impact on your work flow.

osdf commented 8 years ago

Hmm, you do not get the errors mentioned in #63 when doing C-c C-c?

fanpeng-kong commented 8 years ago

Yes, I have the same errors mentioned in #63 if I do not set python-shell-interpreter specifically apart from the parse HTTP error.