Szablon/json_script.ipynb

3.1 KiB

#procedura napisywania plików ipynb (generowanie nagłówka i metadanych)
import json

def modjup(filen,numer,tytul,typ,author,email,lang,title,year):
    zerocell=['![Logo 1](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech1.jpg)\n',
              '<div class="alert alert-block alert-info">\n', 
              '<h1> %s </h1>\n'%(title),
              '<h2> %s. <i>%s</i>  [%s]</h2> \n'%(numer,tytul,typ),
              '<h3> %s (%s)</h3>\n'%(author,year),
              '</div>\n',
              '\n',
              '![Logo 2](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech2.jpg)']
    zerodict={'cell_type': 'markdown','metadata': {'collapsed': False},'source': zerocell}
    with open(filen, 'r+',encoding='utf-8') as f:
        ll=json.load(f)
        ll["metadata"]["author"]=author
        ll["metadata"]["email"]=email
        ll["metadata"]["lang"]=lang
        subtitle="%s.%s[%s]"%(numer,tytul,typ)
        ll["metadata"]["subtitle"]=subtitle
        ll["metadata"]["title"]=title
        ll["metadata"]["year"]=year
    
        if not(ll['cells'][0]['source'][0]==zerocell[0]):
            ll['cells'].insert(0,zerodict)
        else:
            ll['cells'][0]=zerodict
        f.seek(0)
        json.dump(ll,f,indent=4)
#zmodyfikuj te dane
filen="szablon-1.ipynb"

numer="Numer"
tytul="Tytul materiału"
typ="laboratoria/wykład"

author="Imię Nazwisko"
email="adres@email.com"
lang= "pl"
title="Nazwa przedmiotu"
year="Rok"

#uruchom procedurę
modjup(filen,numer,tytul,typ,author,email,lang,title,year)