Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for C# and F# #429

Merged
merged 12 commits into from
Feb 16, 2020
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It can also convert these documents **into** Jupyter
Notebooks, allowing you to synchronize content in both
directions.

The languages that are currently supported by Jupytext are: Julia, Python, R, Bash, Scheme, Clojure, Matlab, Octave, C++, q/kdb+, IDL, TypeScript, Javascript, Scala, Rust/Evxcr, PowerShell and Robot Framework. Extending Jupytext to more languages should be easy - read more at [CONTRIBUTING.md](https://github.com/mwouts/jupytext/blob/master/CONTRIBUTING.md). In addition, jupytext users can choose between two formats for notebooks as scripts:
The languages that are currently supported by Jupytext are: Julia, Python, R, Bash, Scheme, Clojure, Matlab, Octave, C++, q/kdb+, IDL, TypeScript, Javascript, Scala, Rust/Evxcr, PowerShell, C#, F#, and Robot Framework. Extending Jupytext to more languages should be easy - read more at [CONTRIBUTING.md](https://github.com/mwouts/jupytext/blob/master/CONTRIBUTING.md). In addition, jupytext users can choose between two formats for notebooks as scripts:
- The `percent` format, compatible with several IDEs, including Spyder, Hydrogen, VScode and PyCharm. In that format, cells are delimited with a commented `%%`.
- The `light` format, designed for this project. Use that format to open standard scripts as notebooks, or to save notebooks as scripts with few cell markers - none when possible.

Expand Down
2 changes: 1 addition & 1 deletion docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Jupytext can save Jupyter notebooks as
- Markdown and R Markdown documents,
- Scripts in many languages.

The languages that are currently supported by Jupytext are: Julia, Python, R, Bash, Scheme, Clojure, Matlab, Octave, C++, q/kdb+, IDL, TypeScript, Javascript, Scala, Rust/Evxcr, PowerShell and Robot Framework. Extending Jupytext to more languages should be easy - read more at [CONTRIBUTING.md](https://github.com/mwouts/jupytext/blob/master/CONTRIBUTING.md#). In addition, jupytext users can choose between two formats for notebooks as scripts:
The languages that are currently supported by Jupytext are: Julia, Python, R, Bash, Scheme, Clojure, Matlab, Octave, C++, q/kdb+, IDL, TypeScript, Javascript, Scala, Rust/Evxcr, PowerShell, C#, F#,P and Robot Framework. Extending Jupytext to more languages should be easy - read more at [CONTRIBUTING.md](https://github.com/mwouts/jupytext/blob/master/CONTRIBUTING.md#). In addition, jupytext users can choose between two formats for notebooks as scripts:
- The `percent` format, compatible with several IDEs, including Spyder, Hydrogen, VScode and PyCharm. In that format, cells are delimited with a commented `%%`.
- The `light` format, designed for this project. Use that format to open standard scripts as notebooks, or to save notebooks as scripts with few cell markers - none when possible.

Expand Down
8 changes: 5 additions & 3 deletions jupytext/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
'.ts': {'language': 'typescript', 'comment': '//'},
'.scala': {'language': 'scala', 'comment': '//'},
'.rs': {'language': 'rust', 'comment': '//'},
'.robot': {'language': 'robotframework', 'comment': '#'}}
'.robot': {'language': 'robotframework', 'comment': '#'},
'.cs': {'language': 'csharp', 'comment': '//'},
'.fs': {'language': 'fsharp', 'comment': '//'}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two other file extensions for F#:

  • .fsx - F# scripting file, can be loaded directly into the F# Interactive process
  • .fsi - F# interface file (similar to OCAML interface files), used mostly in very large codebases to encapsulate code constructs

.fsi is probably not relevant to this project, but .fsx might be.


_COMMENT_CHARS = [_SCRIPT_EXTENSIONS[ext]['comment'] for ext in _SCRIPT_EXTENSIONS if
_SCRIPT_EXTENSIONS[ext]['comment'] != '#']
Expand All @@ -48,14 +50,14 @@ def default_language_from_metadata_and_ext(metadata, ext):
if language.startswith('C++'):
return 'c++'

return language.lower()
return language.lower().replace('#', 'sharp')


def same_language(kernel_language, language):
"""Are those the same language?"""
if kernel_language == language:
return True
if kernel_language.lower() == language:
if kernel_language.lower().replace('#', 'sharp') == language:
return True
if kernel_language.startswith('C++') and language == 'c++':
return True
Expand Down
2 changes: 1 addition & 1 deletion jupytext/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Jupytext's version number"""

__version__ = '1.3.3'
__version__ = '1.3.3+dev'
152 changes: 152 additions & 0 deletions tests/notebooks/ipynb_cs/csharp.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start with..."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World!\r\n"
]
}
],
"source": [
"Console.WriteLine(\"Hello World!\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we do a plot with Plotly, following the [Plotting with XPlot](https://github.com/dotnet/interactive/blob/master/NotebookExamples/csharp/Docs/Plotting%20with%20Xplot.ipynb) example from `dotnet/interactive`:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div id=\"9a70b50f-ec94-4c20-8fa8-e14290eb0440\" style=\"width: 900px; height: 500px;\"></div>\r\n",
"<script type=\"text/javascript\">\r\n",
"\r\n",
"var renderPlotly = function() {\r\n",
" var xplotRequire = requirejs.config({context:'xplot-3.0.1',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});\r\n",
" xplotRequire(['plotly'], function(Plotly) {\r\n",
"\n",
" var data = [{\"type\":\"bar\",\"x\":[\"A\",\"B\",\"C\"],\"y\":[1,3,2],\"name\":\"Bar\"}];\n",
" var layout = {\"title\":\"A bar plot\"};\n",
" Plotly.newPlot('9a70b50f-ec94-4c20-8fa8-e14290eb0440', data, layout);\n",
" \r\n",
"});\r\n",
"};\r\n",
"if ((typeof(requirejs) !== typeof(Function)) || (typeof(requirejs.config) !== typeof(Function))) { \r\n",
" var script = document.createElement(\"script\"); \r\n",
" script.setAttribute(\"src\", \"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js\"); \r\n",
" script.onload = function(){\r\n",
" renderPlotly();\r\n",
" };\r\n",
" document.getElementsByTagName(\"head\")[0].appendChild(script); \r\n",
"}\r\n",
"else {\r\n",
" renderPlotly();\r\n",
"}\r\n",
"</script>\r\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"using XPlot.Plotly;\n",
"\n",
"var bar = new Graph.Bar\n",
"{\n",
" name = \"Bar\",\n",
" x = new[] {'A', 'B', 'C'},\n",
" y = new[] {1, 3, 2}\n",
"};\n",
"\n",
"var chart = Chart.Plot(new[] {bar});\n",
"chart.WithTitle(\"A bar plot\");\n",
"display(chart);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also test the math outputs as in the [Math and Latex](https://github.com/dotnet/interactive/blob/master/NotebookExamples/csharp/Docs/Math%20and%20LaTeX.ipynb) example:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"\\begin{align} e^{i \\pi} = -1\\end{align}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(LaTeXString)@\"\\begin{align} e^{i \\pi} = -1\\end{align}\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$e^{i \\pi} = -1$$"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(MathString)@\"e^{i \\pi} = -1\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
88 changes: 88 additions & 0 deletions tests/notebooks/ipynb_fs/fsharp.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was inspired by [Plottting with XPlot](https://github.com/dotnet/interactive/blob/master/NotebookExamples/fsharp/Docs/Plotting%20with%20Xplot.ipynb)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"open XPlot.Plotly"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div id=\"401733f6-77ba-44c5-bb00-14b2beb80964\" style=\"width: 900px; height: 500px;\"></div>\r\n",
"<script type=\"text/javascript\">\r\n",
"\r\n",
"var renderPlotly = function() {\r\n",
" var xplotRequire = requirejs.config({context:'xplot-3.0.1',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});\r\n",
" xplotRequire(['plotly'], function(Plotly) {\r\n",
"\n",
" var data = [{\"type\":\"bar\",\"x\":[\"A\",\"B\",\"C\"],\"y\":[1,3,2],\"name\":\"Bar 1\"}];\n",
" var layout = {\"title\":\"A sample bar plot\"};\n",
" Plotly.newPlot('401733f6-77ba-44c5-bb00-14b2beb80964', data, layout);\n",
" \r\n",
"});\r\n",
"};\r\n",
"if ((typeof(requirejs) !== typeof(Function)) || (typeof(requirejs.config) !== typeof(Function))) { \r\n",
" var script = document.createElement(\"script\"); \r\n",
" script.setAttribute(\"src\", \"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js\"); \r\n",
" script.onload = function(){\r\n",
" renderPlotly();\r\n",
" };\r\n",
" document.getElementsByTagName(\"head\")[0].appendChild(script); \r\n",
"}\r\n",
"else {\r\n",
" renderPlotly();\r\n",
"}\r\n",
"</script>\r\n"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"let bar =\n",
" Bar(\n",
" name = \"Bar 1\",\n",
" x = [\"A\"; \"B\"; \"C\"],\n",
" y = [1; 3; 2])\n",
"\n",
"[bar]\n",
"|> Chart.Plot\n",
"|> Chart.WithTitle \"A sample bar plot\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (F#)",
"language": "F#",
"name": ".net-fsharp"
},
"language_info": {
"file_extension": ".fs",
"mimetype": "text/x-fsharp",
"name": "C#",
"pygments_lexer": "fsharp",
"version": "4.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
40 changes: 40 additions & 0 deletions tests/notebooks/mirror/ipynb_to_Rmd/csharp.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
jupyter:
kernelspec:
display_name: .NET (C#)
language: C#
name: .net-csharp
---

We start with...

```{csharp}
Console.WriteLine("Hello World!");
```

Then we do a plot with Plotly, following the [Plotting with XPlot](https://github.com/dotnet/interactive/blob/master/NotebookExamples/csharp/Docs/Plotting%20with%20Xplot.ipynb) example from `dotnet/interactive`:

```{csharp}
using XPlot.Plotly;

var bar = new Graph.Bar
{
name = "Bar",
x = new[] {'A', 'B', 'C'},
y = new[] {1, 3, 2}
};

var chart = Chart.Plot(new[] {bar});
chart.WithTitle("A bar plot");
display(chart);
```

We also test the math outputs as in the [Math and Latex](https://github.com/dotnet/interactive/blob/master/NotebookExamples/csharp/Docs/Math%20and%20LaTeX.ipynb) example:

```{csharp}
(LaTeXString)@"\begin{align} e^{i \pi} = -1\end{align}"
```

```{csharp}
(MathString)@"e^{i \pi} = -1"
```
25 changes: 25 additions & 0 deletions tests/notebooks/mirror/ipynb_to_Rmd/fsharp.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
jupyter:
kernelspec:
display_name: .NET (F#)
language: F#
name: .net-fsharp
---

This notebook was inspired by [Plottting with XPlot](https://github.com/dotnet/interactive/blob/master/NotebookExamples/fsharp/Docs/Plotting%20with%20Xplot.ipynb).

```{fsharp}
open XPlot.Plotly
```

```{fsharp}
let bar =
Bar(
name = "Bar 1",
x = ["A"; "B"; "C"],
y = [1; 3; 2])

[bar]
|> Chart.Plot
|> Chart.WithTitle "A sample bar plot"
```
39 changes: 39 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/csharp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ---
// jupyter:
// kernelspec:
// display_name: .NET (C#)
// language: C#
// name: .net-csharp
// ---

// %% [markdown]
// We start with...

// %%
Console.WriteLine("Hello World!");

// %% [markdown]
// Then we do a plot with Plotly, following the [Plotting with XPlot](https://github.com/dotnet/interactive/blob/master/NotebookExamples/csharp/Docs/Plotting%20with%20Xplot.ipynb) example from `dotnet/interactive`:

// %%
using XPlot.Plotly;

var bar = new Graph.Bar
{
name = "Bar",
x = new[] {'A', 'B', 'C'},
y = new[] {1, 3, 2}
};

var chart = Chart.Plot(new[] {bar});
chart.WithTitle("A bar plot");
display(chart);

// %% [markdown]
// We also test the math outputs as in the [Math and Latex](https://github.com/dotnet/interactive/blob/master/NotebookExamples/csharp/Docs/Math%20and%20LaTeX.ipynb) example:

// %%
(LaTeXString)@"\begin{align} e^{i \pi} = -1\end{align}"

// %%
(MathString)@"e^{i \pi} = -1"
Loading