Mantid
Loading...
Searching...
No Matches
NotebookWriter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <utility>
8
10#include "MantidJson/Json.h"
11#include "MantidKernel/Logger.h"
13
14namespace Mantid::API {
15
16namespace {
17Mantid::Kernel::Logger g_log("NotebookWriter");
18}
19
20NotebookWriter::NotebookWriter() : m_cell_buffer(Json::arrayValue) {
21 // Add header comments and code
23 headerCode();
24}
25
32void NotebookWriter::codeCell(Json::Value array_code) {
33 Json::Value cell_data;
34 const Json::Value empty = Json::Value(Json::ValueType::objectValue);
35
36 cell_data["cell_type"] = "code";
37 cell_data["collapsed"] = false;
38 cell_data["input"] = std::move(array_code);
39 cell_data["language"] = "python";
40 cell_data["metadata"] = empty;
41 cell_data["outputs"] = Json::Value(Json::arrayValue);
42
43 m_cell_buffer.append(cell_data);
44}
45
51std::string NotebookWriter::codeCell(const std::string &string_code) {
52 Json::Value cell_data;
53 const Json::Value empty = Json::Value(Json::ValueType::objectValue);
54
55 cell_data["cell_type"] = "code";
56 cell_data["collapsed"] = false;
57 cell_data["input"] = string_code;
58 cell_data["language"] = "python";
59 cell_data["metadata"] = empty;
60 cell_data["outputs"] = Json::Value(Json::arrayValue);
61
62 m_cell_buffer.append(cell_data);
63 return Mantid::JsonHelpers::jsonToString(cell_data, " ");
64}
65
72void NotebookWriter::markdownCell(Json::Value string_array) {
73 Json::Value cell_data;
74 const Json::Value empty = Json::Value(Json::ValueType::objectValue);
75
76 cell_data["cell_type"] = "markdown";
77 cell_data["metadata"] = empty;
78 cell_data["source"] = std::move(string_array);
79
80 m_cell_buffer.append(cell_data);
81}
82
88std::string NotebookWriter::markdownCell(const std::string &string_text) {
89 Json::Value cell_data;
90 const Json::Value empty = Json::Value(Json::ValueType::objectValue);
91
92 cell_data["cell_type"] = "markdown";
93 cell_data["metadata"] = empty;
94 cell_data["source"] = string_text;
95
96 m_cell_buffer.append(cell_data);
97 return Mantid::JsonHelpers::jsonToString(cell_data, " ");
98}
99
105 Json::Value strings(Json::arrayValue);
106 strings.append(Json::Value("This IPython Notebook was automatically "
107 "generated by MantidPlot, version: "));
108 strings.append(Json::Value(Mantid::Kernel::MantidVersion::version()));
109 strings.append(Json::Value("\n"));
110 strings.append(Json::Value(Mantid::Kernel::MantidVersion::releaseNotes()));
111 strings.append(Json::Value("\n\nThe following information may be useful:\n"
112 "* [Mantid Framework Python API Reference]"
113 "(http://docs.mantidproject.org/nightly/api/python/index.html)\n"
114 "* [IPython Notebook "
115 "Documentation](http://ipython.org/ipython-doc/stable/notebook/)\n"
116 "* [matplotlib Documentation](http://matplotlib.org/contents.html)\n\n"
117 "Help requests and bug reports should be submitted to the [Mantid forum.]"
118 "(http://forum.mantidproject.org)"));
119
120 markdownCell(strings);
121}
122
130 Json::Value import_mantid(Json::arrayValue);
131
132 import_mantid.append(Json::Value("#Import Mantid's Python API and IPython plotting tools\n"
133 "from mantid.simpleapi import *\n"
134 "from MantidIPython import *\n"
135 "\n"
136 "#Some magic to tell matplotlib how to behave in IPython Notebook. Use "
137 "'%matplotlib nbagg' for interactive plots, if available.\n"
138 "%matplotlib inline"));
139
140 codeCell(import_mantid);
141}
142
148 Json::Value output;
149 const Json::Value empty = Json::Value(Json::ValueType::objectValue);
150
151 Json::Value worksheet;
152 worksheet["cells"] = m_cell_buffer;
153 worksheet["metadata"] = empty;
154
155 Json::Value worksheet_arr(Json::arrayValue);
156 worksheet_arr.append(worksheet);
157
158 Json::Value meta_name;
159 meta_name["name"] = "Mantid Notebook";
160 output["metadata"] = meta_name;
161 output["nbformat"] = 3;
162 output["nbformat_minor"] = 0;
163 output["worksheets"] = worksheet_arr;
164
165 return output;
166}
167
174 const Json::Value root = buildNotebook();
175
176 return Mantid::JsonHelpers::jsonToString(root, " ");
177}
178} // namespace Mantid::API
void headerComment()
Add a markdown cell of information for the user to the buffer of cells to write to the notebook.
void headerCode()
Add code cells to the buffer of cells to write to the notebook These are to import Mantid and matplot...
Json::Value buildNotebook()
Create a Json value containing the whole notebook.
std::string codeCell(const std::string &string_code)
Add a code cell to the buffer of cells to write to the notebook.
std::string writeNotebook()
Create a formatted string of Json which describes a notebook.
std::string markdownCell(const std::string &string_text)
Add a markdown cell to the buffer of cells to write to the notebook.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
static const char * version()
The full version number.
static std::string releaseNotes()
The url to the most applicable release notes.
Definition: Algorithm.h:38
Kernel::Logger g_log("ExperimentInfo")
static logger object