Skip to content

Commit

Permalink
- update code_imports dictionary
Browse files Browse the repository at this point in the history
- update example
- update readme
  • Loading branch information
luiztauffer committed Apr 20, 2020
1 parent 06d267e commit eef7a07
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,35 @@ $ pip install qtvoila

## Usage

QtVoila should be imported and used as a PySide2 widget:
QtVoila should be imported and used as a PySide2 widget, anywhere inside your GUI application. Although it can be initialized with default parameters, you are able to define the **parent** (the PySide2 application), the **temporary directory** where any created notebooks will be temporarily stored, the path to an existing **external notebook** and the boolean option to either strip code sources on Voila rendering or not:

```python
from qtvoila import QtVoila

voila_widget = QtVoila()
voila_widget = QtVoila(
parent=None,
temp_dir=None,
external_notebook=None,
strip_sources=True
)
```

To pass the Python code to be processed by Jupyter:
If creating a notebook programmatically, new cells can be added with the method `add_notebook_cell()`. This method accepts three arguments: `code_imports` is a dictionary of modules to be imported, `code` is the string containing the cell's code or markdown text and `cell_type` defines if the cell is of type code or markdown. Examples:

```python
code = "here goes your python code"
voila_widget.code = code
# Mardown cell
mtext = "#This is my title\n"
mtext += "Here goes some text. Check out this graphic:"
voila_widget.add_notebook_cell(code=mtext, cell_type='markdown')

# Code cell
imports = {
'matplotlib': ['pyplot'],
'numpy': [],
}
code = "%matplotlib inline\n"
code += "pyplot.plot(numpy.random.rand(10))"
voila_widget.add_notebook_cell(code_imports=imports, code=code, cell_type='code')
```

To run the Voila process and render the result on widget:
Expand All @@ -42,6 +59,6 @@ voila_widget.close_renderer()

## Examples

Find the code for the example below at [examples](https://github.com/luiztauffer/qtvoila/tree/master/examples):
[Here](https://github.com/luiztauffer/qtvoila/tree/master/examples) you can find examples on how to use QtVoila in your PySide2 application.

![](assets/gif_matplotlib_example.gif)
13 changes: 7 additions & 6 deletions examples/simple_window_with_qtvoila.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,24 @@ def __init__(self, parent=None):
self.show()

def pass_code_to_voila_widget(self):
self.voila_widget.external_notebook = None
code1 = self.edit1.toPlainText()
if self.r0.isChecked():
self.voila_widget.add_notebook_cell_code(code=code1, cell_type='code')
self.voila_widget.add_notebook_cell(code=code1, cell_type='code')
else:
self.voila_widget.add_notebook_cell_code(code=code1, cell_type='markdown')
self.voila_widget.add_notebook_cell(code=code1, cell_type='markdown')

code2 = self.edit2.toPlainText()
if self.r2.isChecked():
self.voila_widget.add_notebook_cell_code(code=code2, cell_type='code')
self.voila_widget.add_notebook_cell(code=code2, cell_type='code')
else:
self.voila_widget.add_notebook_cell_code(code=code2, cell_type='markdown')
self.voila_widget.add_notebook_cell(code=code2, cell_type='markdown')

code3 = self.edit3.toPlainText()
if self.r4.isChecked():
self.voila_widget.add_notebook_cell_code(code=code3, cell_type='code')
self.voila_widget.add_notebook_cell(code=code3, cell_type='code')
else:
self.voila_widget.add_notebook_cell_code(code=code3, cell_type='markdown')
self.voila_widget.add_notebook_cell(code=code3, cell_type='markdown')
# Run Voila
self.voila_widget.run_voila()

Expand Down
7 changes: 5 additions & 2 deletions qtvoila/qtvoila.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, parent=None, temp_dir=None,
self.internal_notebook = nbf.v4.new_notebook()
self.internal_notebook['cells'] = []

def add_notebook_cell_code(self, code_imports={}, code="", cell_type='code'):
def add_notebook_cell(self, code_imports={}, code="", cell_type='code'):
"""
Adds new cell to run on a Jupyter Notebook.
Expand All @@ -47,7 +47,10 @@ def add_notebook_cell_code(self, code_imports={}, code="", cell_type='code'):
# Imports extension modules
imports_code = ""
for k, v in code_imports.items():
imports_code += "from " + k + " import " + ", ".join(v) + "\n"
if len(v) > 0:
imports_code += "from " + k + " import " + ", ".join(v) + "\n"
else:
imports_code += "import " + k + "\n"
code = imports_code + code
# Make notebook cell
if cell_type == 'code':
Expand Down

0 comments on commit eef7a07

Please sign in to comment.