Package: hkjournalist
HK-journalist: Custom Automatic Report Generator for Python program!It is a light and useful Python module, helping you generate a small size, pretty report as PDF slides (or any other format documents which humans can directly read and hand out) each time after your programs finish. All you need to do is to customize a report template usingMarkDownwith variables names used in your Python program and maintain adictto store those variables. Then, A few lines of code added before the end of programs can automatically fetch and display them in the final report file. Also, the codes intended to deal with frequent structures/arguments changes or data source changes can benefit from the package if the report can play a role of 'snapshot' (with timestamp) of each code version.Documentation:https://hk-journalist.readthedocs.io/en/latest/index.htmlSlides for sharing:slidesTable of ContentsQuick StartInstallCustomize your report templateRun aJournalist()in your code to fetch variablesInvite a journalist to make a big news reportI am too lazy to write amdtemplateSupport For zh_CN(New feature :tada:)What will my variables on slides look like?More examples and instructionsTipsPrerequirementsQuick StartBefore installinghkjournalist, please make surepandocandpdflatexare already properly installed in the environment. (install instruction)Installpip install hkjournalistCustomize your report templateWrite such anmdfile, use a pair of{}to wrap every variable which will be assigned a specified value in your code. save it totemplate.md% Hello World
% Xinyi Li
% 2019-12-19

---### sine plot![]({sin_plot})### sine table{sin_table}### sine function```{{.python}}
{sin_func}
```Run aJournalist()in your code to fetch variablesFirst, you should define adictto record mapping with variable names and their valuefromhkjournalistimportJournalistconfig={}Then, start your programming, and do not forget to assign value to corresponding variable names inconfig:defsin_2x_and_cos_2x(x):y=np.sin(x)*np.sin(x)+np.cos(x)*np.cos(x)returnyx=np.arange(0,4*np.pi,0.1)y1=np.sin(x)y2=np.cos(x)df=pd.DataFrame({'x':x,'sin(x)':y1,'cos(x)':y2})df['sin^2^(x)+cos^2^(x)']=sin_2x_and_cos_2x(df['x']).valuesdf=df.set_index('x')# plot sine curve as sin_plotax=df.plot()plt.tight_layout()config['sin_plot']=ax# random select 5 point (x,y) as sin_tableconfig['sin_table']=df.sample(5)# sin_2x_and_cos_2x as sin_funcconfig['sin_func']=sin_2x_and_cos_2xInvite a journalist to make a big news reportLast but not least, attach 3 linescriticalcode below to have yourJournalistmake a report and save it tobig_news.pdf(you can get all code above indemoand the output filebig_news.pdf)# HK journalist runs faster than everyone! hear variable and make a reportreporter=Journalist(template_file='template.md')reporter.hear(config)reporter.report(output_file='big_news.pdf',beamer=True,overwrite=True)Report slides display as below:I am too lazy to write anmdtemplateIf you have too many variables to report, which make write a template a big project, or simply don't want to write anmdtemplate,No problem!hkjournalistcan generate a report template with each variable on one slide page automatically. With slight modification or directly using it as a template, you can get your real report.Exampleimportnumpyasnpimportmatplotlib.pyplotaspltimportseabornassnsfromhkjournalistimportJournalistconfig={}foriinrange(1,4):uniform_data=np.random.rand(10,12)plt.figure(figsize=(10,8))ax=sns.heatmap(uniform_data,cmap='Blues',annot=True,linewidth=.5)plt.tight_layout()config[f'Plot_{i}']=axreporter=Journalist(fig_height='80%')reporter.hear(config)reporter.generate_template('auto_generate_template.md')reporter.report(output_file='auto_report.pdf',beamer=True,overwrite=True)Output (raw file):Support For zh_CNYes, now, from version 0.0.5, it can use template contains chinese characters :tada:.Just pass argumentzh=Truewhen creating a Journalist(), or set itszhproperty at any time before generating reports.reporter=Journalist(template_file='./reports/template.md',zh=True)If you want to use your customized template with chinese characters, don't forget to add one line at first of the YAML metadata block in yourMarkdowntemplate:---
documentclass: ctexbeamer...---Here is asimple examplesimilar toQuick start, Except forits templatecontaining chinese characters. A perfect chineseslidesfile can be generated by the program as:Note: make surexelatexis installed properly.More details can be found inAPI Documentationandthis tutorial.What will my variables on slides look like?All variables pass toJournalistviahearwill display as strings just like what their__str__method does.Except for 4 types with special support:pandas.DataFrame: -> a 3-line table (TeXdefault style)matplotlib.axes.SubplotBase(known as base figure objectaxinmatplotlib):  -> a figure print on report (with high quality and small size aspdf)function: -> its full definitionlist(str): ->len(list)followed by a sentance with all words concatenated.Besides,list(str)andpandas.DataFramemay be too long to display, which are allowed to span multiple pages in final slides.More examples and instructions[Documentation(https://hk-journalist.readthedocs.io/)More complex usages available inexamplesHow to write a sophisticated slides template usingmdï¼Ÿ Seepandoc tutorialsTipsBefore passplt.Axesobject (figure object used inmatplotlib) todict, useplt.tight_layout()statement to adjust its size.Use double curly braces{{}}to escape{}in the template.Turn off parameteroverwriteinJournalist.report(), you will get a file name with a timestamp in it. All these reports generated by such a method serve as snapshots.To produce plain PDF article document instead of beamer slides, turn offbeamerinreport().Prerequirementspandoc:https://pandoc.org/installing.htmltexlive/mactex(for MacOS):https://www.tug.org/texlive/http://www.tug.org/mactex/And if you use some IDEs like PyCharm, you need to add their path (usewhich texandwhich pandocin bash environment to find them) to environment variables$PATHin IDE run options. (e.g. in Pycharm,run->edit configuration->environment variables)ForJupyter Notebookusers, the final output file may not be opened on Chrome (seethis question). So, for convenience, you can downloadFirefoxto write notebooks and view pdf reports right after generating.