Zim

Store page to the notebook using Notebook API

Asked by sreekar

Using Notebook API, I want to store a page with content to a notebook. But I am not able to store the page to the notebook. The script I tried is as below:

import zim.notebook as zn
import zim.fs as fs
f_path = fs.FilePath('/home/fubar/Journals2') #folder path for the notebook "Journals"
dir_nb = fs.Dir(f_path) #create Dir object to store notebook contents
nb = zn.Notebook(dir = dir_nb) #create the Notebook with directory
# now let us create a New Page
#check for correct path by resolving the path
path = nb.resolve_path('thispaper') #check if path to the page :thispaper is valid
print 'name of this page is ' + path.name
newpage = zn.Page(path) #use this path to create a page
newpage.readonly = False #set the page to write-able mode
text_content = 'some content for the page' #content for the page
newpage.parse(format = 'wiki',text = text_content) #parse the content according to wiki and add to page
nb.store_page(newpage) #save the content of page to storage backend

I get this error
*******************************
No handlers could be found for logger "zim.notebook"
name of this page is thispaper
Traceback (most recent call last):
  File "create_notebook_zim.py", line 14, in <module>
    nb.store_page(newpage) #save the content of page to storage backend
  File "/usr/lib/python2.7/dist-packages/zim/notebook.py", line 1343, in store_page
    store.store_page(page)
  File "/usr/lib/python2.7/dist-packages/zim/stores/files.py", line 104, in store_page
    page._store()
AttributeError: 'Page' object has no attribute '_store'
*******************************

I expect there for a folder and file name "thispaper" to create in /home/fubar/Journals2 directory

Question information

Language:
English Edit question
Status:
Solved
For:
Zim Edit question
Assignee:
No assignee Edit question
Solved by:
Jaap Karssenberg
Solved:
Last query:
Last reply:
Revision history for this message
Best Jaap Karssenberg (jaap.karssenberg) said :
#1

Your code looks quit reasonable. But it goes wrong because the backend uses
a sub-class of Page instead of the Page class itself.

To solve this you should not construct the Page object yourself. Instead
use "Notebook.get_page(path)" to construct the new page.

Regards,

Jaap

On Sun, Oct 26, 2014 at 12:06 AM, sreekar <
<email address hidden>> wrote:

> New question #256193 on Zim:
> https://answers.launchpad.net/zim/+question/256193
>
> Using Notebook API, I want to store a page with content to a notebook. But
> I am not able to store the page to the notebook. The script I tried is as
> below:
>
> import zim.notebook as zn
> import zim.fs as fs
> f_path = fs.FilePath('/home/fubar/Journals2') #folder path for
> the notebook "Journals"
> dir_nb = fs.Dir(f_path)
> #create Dir object to store notebook contents
> nb = zn.Notebook(dir = dir_nb)
> #create the Notebook with directory
> # now let us create a New Page
> #check for correct path by resolving the path
> path = nb.resolve_path('thispaper')
> #check if path to the page :thispaper is valid
> print 'name of this page is ' + path.name
> newpage = zn.Page(path)
> #use this path to create a page
> newpage.readonly = False
> #set the page to write-able mode
> text_content = 'some content for the page' #content
> for the page
> newpage.parse(format = 'wiki',text = text_content) #parse the content
> according to wiki and add to page
> nb.store_page(newpage)
> #save the content of page to storage backend
>
> I get this error
> *******************************
> No handlers could be found for logger "zim.notebook"
> name of this page is thispaper
> Traceback (most recent call last):
> File "create_notebook_zim.py", line 14, in <module>
> nb.store_page(newpage)
> #save the content of page to storage backend
> File "/usr/lib/python2.7/dist-packages/zim/notebook.py", line 1343, in
> store_page
> store.store_page(page)
> File "/usr/lib/python2.7/dist-packages/zim/stores/files.py", line 104,
> in store_page
> page._store()
> AttributeError: 'Page' object has no attribute '_store'
> *******************************
>
> I expect there for a folder and file name "thispaper" to create in
> /home/fubar/Journals2 directory
>
>
> --
> You received this question notification because you are an answer
> contact for Zim.
>

Revision history for this message
sreekar (colonel-sreekar) said :
#2

Thanks Jaap Karssenberg, that solved my question.