emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: emacs-orgmode@gnu.org
Subject: Re: documenting python module examples in orgmode
Date: Mon, 09 Dec 2019 09:19:40 -0500	[thread overview]
Message-ID: <m2d0cxmvib.fsf@LAPTOP-8C07ES6L.wv.cc.cmu.edu> (raw)
In-Reply-To: <87k175dbr1.fsf@santanas.co.za>

The only way to do this is if you have an intermediate tangle step so
that the python file exists in a place where it can be imported. For
example, this small modification will make what you want happen.


#+NAME: my_module.py
#+begin_src python :tangle my_module.py
print('Importing my_module...')

test = 'Test String'

def find_index(to_search, target):
    '''Find the index of a value in a sequence'''
    for i, value in enumerate(to_search):
        if value == target:
            return i

    return -1
#+end_src

#+NAME: intro.py
#+begin_src python :var tangle=(org-babel-tangle)
import my_module

courses = [ 'History', 'Math', 'Geography', 'Science' ]
#+end_src

#+RESULTS: intro.py
: Importing my_module...



Divan Santana <divan@santanas.co.za> writes:

> Hi All,
>
> I'm trying to document python module examples in orgmode.
>
> I'm sure this is possible, but not quite sure how to do it.
>
> I'd like to define the python module in one block, and then import it in
> another.
>
> #+NAME: my_module.py
> #+begin_src python
> print('Importing my_module...')
>
> test = 'Test String'
>
> def find_index(to_search, target):
>     '''Find the index of a value in a sequence'''
>     for i, value in enumerate(to_search):
>         if value == target:
>             return i
>
>     return -1
> #+end_src
>
> #+NAME: intro.py
> #+begin_src python
> import my_module
>
> courses = [ 'History', 'Math', 'Geography', 'Science' ]
> #+end_src
>
> How can one do this?
>
> Thanks very much.


--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

  reply	other threads:[~2019-12-09 14:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-09 10:38 documenting python module examples in orgmode Divan Santana
2019-12-09 14:19 ` John Kitchin [this message]
2019-12-09 14:25 ` Kaushal Modi
2019-12-11 12:44 ` Divan Santana

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2d0cxmvib.fsf@LAPTOP-8C07ES6L.wv.cc.cmu.edu \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).