emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* documenting python module examples in orgmode
@ 2019-12-09 10:38 Divan Santana
  2019-12-09 14:19 ` John Kitchin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Divan Santana @ 2019-12-09 10:38 UTC (permalink / raw)
  To: emacs-orgmode

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: documenting python module examples in orgmode
  2019-12-09 10:38 documenting python module examples in orgmode Divan Santana
@ 2019-12-09 14:19 ` John Kitchin
  2019-12-09 14:25 ` Kaushal Modi
  2019-12-11 12:44 ` Divan Santana
  2 siblings, 0 replies; 4+ messages in thread
From: John Kitchin @ 2019-12-09 14:19 UTC (permalink / raw)
  To: emacs-orgmode

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: documenting python module examples in orgmode
  2019-12-09 10:38 documenting python module examples in orgmode Divan Santana
  2019-12-09 14:19 ` John Kitchin
@ 2019-12-09 14:25 ` Kaushal Modi
  2019-12-11 12:44 ` Divan Santana
  2 siblings, 0 replies; 4+ messages in thread
From: Kaushal Modi @ 2019-12-09 14:25 UTC (permalink / raw)
  To: Divan Santana; +Cc: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 643 bytes --]

On Mon, Dec 9, 2019, 5:49 AM Divan Santana <divan@santanas.co.za> wrote:

> 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.
>

Have you looked at the Noweb syntax?

It might so what you want (inject code from one Org src block into multiple
other src blocks).

Here's an example from my notes where I use Noweb syntax with both Python
and Nim src blocks:
https://gitlab.com/kaushalmodi/kaushalmodi.gitlab.io/raw/master/content-org/notes/string-fns-nim-vs-python.org

>

[-- Attachment #2: Type: text/html, Size: 1384 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: documenting python module examples in orgmode
  2019-12-09 10:38 documenting python module examples in orgmode Divan Santana
  2019-12-09 14:19 ` John Kitchin
  2019-12-09 14:25 ` Kaushal Modi
@ 2019-12-11 12:44 ` Divan Santana
  2 siblings, 0 replies; 4+ messages in thread
From: Divan Santana @ 2019-12-11 12:44 UTC (permalink / raw)
  To: emacs-orgmode

Thanks John Kitchin. Your suggestion works.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-11 13:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-09 10:38 documenting python module examples in orgmode Divan Santana
2019-12-09 14:19 ` John Kitchin
2019-12-09 14:25 ` Kaushal Modi
2019-12-11 12:44 ` Divan Santana

Code repositories for project(s) associated with this 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).