Author Topic: Working with Visio the Geek way - Jupyter/IPython Notebooks  (Read 282 times)

0 Members and 1 Guest are viewing this topic.

Yacine

  • Hero Member
  • *****
  • Posts: 2483
Working with Visio the Geek way - Jupyter/IPython Notebooks
« on: October 03, 2018, 07:55:23 AM »
It's the end of summer, but for me it's already Christmas time.

I've been gifted with a new toy.

It has several names, depending on the point of view: Python, IPython, Jupyter Lab.
  • Python is the programming language
  • IPython is the notebook in your browser, where you can write your code in Python (or other languages if needed)
  • Jupyter lab bundles IPython notebooks in a nicer GUI.
So I've been playing for over a month now with Jupyter Lab and got absolutely addicted.
With it I solved problems, I have on my desk for almost 10 years.
Why? Because:
  • The coding workflow is so pleasant. You get individual cells, which you execute and debug without any intermediate steps - no saving, compiling, switching to other windows, ... You run the cell (ctrl-enter) and watch the result. You do this step by step for the whole process execution, adding one cell after the other. Once all cells execute properly, you merge them, transform the direct assignment into a function and you're done. You get a re-usable piece of code.This work flow reduces the hurdle height to such a minimum, that you won't fear any more attacking problems seeming too complicated.
  • Python is a great language. It is so simple, that you can immediately start coding to get elementary tasks done. Only when formulating more complex requests, you start diving into its depths. It has a community almost as large as Excel's. No matter how you formulate your question, you'll always find an answer (mostly on StackOverflow).The code is overall faster and easier to read.
  • The "Ecosystem" of Python is so large, that you'll hardly encounter  problems none has encountered and solved before. There are libraries for any and every thing. (only Visio has been neglected so far).
So what am I trying to say?
  • IPython for Visio is nothing for regular Visio users, but Visio-Geeks should definitely embrace it.
  • IPython for Visio opens a world of opportunities, worth exploring.
  • There's nothing (almost) about IPython/Visio that one could not solve by means of other tools (C#, VBA, ...), but none of these tools are so convenient. It's this convenience that can make you make this small further step to perfection.
The way there is easy: https://www.anaconda.com/

I'll share my Visio-IPython library in a separate post.
----
What did I end up with?
  • I'll need a third monitor. Additionally to the 2nd monitor showing my access app for editing the shape data, I'll now need a new monitor displaying my IPython notebook.
What did I accomplish so far special?
  • I now can manipulate string parts of shape data values. (In so far important, as in my case I've coded tags in strings)
  • I now can import cell formulas from shapes to excel-sheets and re-import them to other shapes. This statement may seem harmless at a first glance, actually (for myself at least), actually it revolutionaries the world of smart-shapes development.  Instead of behaviour buried in shapesheets, now you can explicitly describe the modifications made to a shape. No more explicit coding of shape cells, but separation of definition and implementation!!!
« Last Edit: October 09, 2018, 12:13:49 PM by Yacine »
Yacine

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3280
  • Ideas Visio-lized into solutions
Re: Working with Visio the Geek way
« Reply #1 on: October 03, 2018, 09:00:32 AM »
Ahh!  What every "I hate programming." individual ought to have!

But, alas, only now has the need so diminished, that the bigger challenge would be finding a use for it.  Bummer!

Well, Merry Christmas!!!  Bake up some holiday goodies...so to speak.

Wapperdude
Visio 2007 Std

Gustavo

  • Jr. Member
  • **
  • Posts: 37
    • OperaGen
Re: Working with Visio the Geek way
« Reply #2 on: October 08, 2018, 01:39:50 PM »
Very interesting your work and experience with Python & Visio Yacine. I've trying to take a look to the technology since you described it some months ago, but the timeline of some deliverables i got, has slowed my learning curve. I think it would have potential to translate the visio data model to some web delivered apps, but I'm not so sure.  An effortless dynamic manipulation of strings in shapesheet cells has been a dream to visio developers, and so far I had to do it using two or more additional cells in the shapesheet. Which other applications do you envision by using these two technologies together? best regards.

Yacine

  • Hero Member
  • *****
  • Posts: 2483
Re: Working with Visio the Geek way
« Reply #3 on: October 09, 2018, 11:30:26 AM »
Hi Gustavo,
very nice question indeed ... for which I can only provide an ambiguous and indirect answer.

There are 3 aspects to consider:
  • The win32com is just a driver making Visio's API available to Python.
    Nothing more than VBA or C# would provide (rather less than more).
  • The Jupyter Notebooks are a cool tool where you can either write small instruction sets on the fly, or re-use such sets (= snippets).
  • Python is a language in which you can make some pretty nice stuff (OOP, big data, excel, databases, web, ...). That makes it easy to combine Visio with other technologies.
So for myself, the scenario would look as follows:
  • Set up a library of Visio functions for re-use.
    These functions are either direct translations of API commands, or small convenient snippets.
  • Using this library (%run myLib.ipynb) I then write notebooks for either single or repetitive tasks.
    For example: I set up a NB for editing the P&IDs I handle on a daily basis.
    Another one did create graphs from indented texts ( http://visguy.com/vgforum/index.php?topic=8530.0 ) / (didn't post the answer yet)
  • I do also use small snippets for formatting shapes. (identical to http://visguy.com/vgforum/index.php?topic=6914.0). I currently prefer calling the respective snippet, than to load the stencil, start the macro, load the script and execute. (subjectively faster)
Future development ideas are:
  • Write more fancy diagram creating scripts (à la D3.js)
  • Use Visio to visualize simulation results for my P&IDs (similar to https://www.youtube.com/watch?v=WpKFGgEjGPg), but with more "smartness" / having behaviour really depend on calculation results.
  • Use Visio as input tool for IPython NBs. Read form inputs, translate shape geometries to data (eg size or position for sorting manually items)
  • Improve the smart shapes creation process, by not only providing an excel version of the shapesheet, but also of the sub-shapes of a shape (very tedious and buggy process)
  • Copy Nikolays export to SVG and HTML + Javascript addin ( http://visguy.com/vgforum/index.php?topic=7642.msg32409#msg32409 ) as to have a transparent tool, one can customize to his needs (Nikolay, I hope you don't mind).
  • Generate PFDs (Process Flow Diagrams) out of design calculations made for process plants of my P&IDs.
    That's basically collecting and aggregating calculation results for different production modes of a plant.
  • Write a Sankey tool similar to Matplotlib's, which I would use to model and display process recipes.
  • Create hierarchical structures for layers and for styles to mimic Autocad's capabilities in these matters.
Basically the applications of the combination Visio/Python are endless. It's your personal preference that will most often dictate the tool of choice. Seldom enough there will be rational reasons for one tool over another. But at equal levels of comfort with all the tools, it's probably Python that you'll use for the next task to accomplish.
I'm sure you can come up with more ideas. I'd love to read you contributions.


So as of today October, 9th 2018, the current version of my Visio library looks as in the attachment ... still in very early stage.There are some initializing scripts, some direct API calls and some snippets I used so far for either honest working or for testing functions.Your suggestions are welcome.
« Last Edit: October 09, 2018, 12:38:27 PM by Yacine »
Yacine

vojo

  • Hero Member
  • *****
  • Posts: 1138
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #4 on: October 09, 2018, 02:11:25 PM »
if it were me, I would consider a whole different approach
- use visio as a basic display pane (similar to tkinter without all the dependences on open GL license)
- user python to create the drawing
- work flow like this
    - call up python function   e.g.  s1 = pysquare (x,y,xpos,ypos)
          - add to list  e.g.   shapelist = list()
                                     shapelist.append(s1)
    - run it thru a bunch a transforms of interest   e.g. 3D==>2D projects, add some text, etc
    - call up pithing function  e.g. c1 = pycircle (r, xpos, ypos)
    - transforms et al
                                      shapelist.append(c1)
    - when all done, call a python function  e.g. pycommit_to_visio (shapelist)
      (this draws the actual shapes)
- put functions in a module
    - squar
    - circle
    - transforms
    -etc.

I am sure you can do it in xml as well...but this removes that step or at least make the process cleaner

Just a thought

Yacine

  • Hero Member
  • *****
  • Posts: 2483
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #5 on: October 10, 2018, 01:38:24 PM »
@vojo,
yes, but this is a step further than where I am now.
For the time being I'm still developing (actually playing) and am my own customer /consumer.
Only once I want to distribute my scripts, I will start writing GUI forms with tkinter or Qt to make them fool proof.
But basically I totally agree with your thoughts.

Cheers, Y.

PS: one additional idea - presenting the script encapsulated in a GUI will of course break totally the idea of the titel ('the Geek way'). This will be the "safe" way.
Yacine

vojo

  • Hero Member
  • *****
  • Posts: 1138
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #6 on: October 10, 2018, 09:27:43 PM »
no no...stay way from tkinter since you would have to include all kinds of extra modules to make it an EXE.
Tkinter is not a single/few self contained/tkinter modules...it assumes the local platform has openCL et al installed. 
it usually does...but that means you would have to ship this as a zip of all your source code.
maybe you want to do that??   that just means that its for pythonistas only...which might be the first/ideal target audience.
(you could look at augmenting pycad with a "visio behavior" module if you want to do more than generatel visio xml).
(conversely, I am pretty sure there are python apps on GitHub that do xml generation...maybe just tailor one of them for visio)

zip of source is, effectively, like shipping 100s of VBA scripts to get this going.  my guess is that you want to learn python
otherwise, mind as well have VBA script library / launcher / add on

I guess I am obtuse...not sure of the value of generating visio XML from python unless that is a first step to a long term plan.
(you don't need any interlock with visio to generate visio xml....python -->xml --> visio....days apart).

sorry for any heartburn  :-)

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3280
  • Ideas Visio-lized into solutions
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #7 on: October 10, 2018, 09:54:13 PM »
Quote
tkinter
   

???  I thought that was a typo …  ;D

@Yacine, Vojo:  you guys are giving me a headache.  And people wonder why I hate programming.   >:(
Visio 2007 Std

Yacine

  • Hero Member
  • *****
  • Posts: 2483
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #8 on: October 11, 2018, 11:20:41 AM »
Quote
tkinter
   

 ???  I thought that was a typo …  ;D

You're kidding us. You cannot pretend hating programming, we've seen your connection macros.

By the way TKinter isn't that "esoteric":
https://www.youtube.com/playlist?list=PL6gx4Cwl9DGBwibXFtPtflztSNPGuIB_d
Yacine

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3280
  • Ideas Visio-lized into solutions
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #9 on: October 11, 2018, 05:59:12 PM »
Being able to do something, doesn't mean you enjoy it. 

And I really don't enjoy it.  But, sometimes, you do what you have to do, do it the best you can...at the time.  But the scope of this thread is way beyond where I can follow.
Visio 2007 Std

Yacine

  • Hero Member
  • *****
  • Posts: 2483
Re: Working with Visio the Geek way - Jupyter/IPython Notebooks
« Reply #10 on: October 12, 2018, 05:13:56 AM »
Being able to do something, doesn't mean you enjoy it.  ...

That's where Python and especially Jupyter Lab jump in.
I do also know how frustrating a difficult coding job can become.
One rough idea, hundreds of debugging loops, short happy moment and 2 days later you wont even recall what, why and how you did it.
In Jupyter Lab you'll have much more small satisfaction moments ... and if you're disciplined enough to comment you code (Markdown cells), you'll be able to understand what you did even months afterward.


... But the scope of this thread is way beyond where I can follow.
... sure but not because it is so difficult, but rather because you haven't look at it yet.
Yes I did watch a whole bunch of youtube videos on the subject, but I had no other help than that. ... and it was enough to get me started.
I would even state that Python is easier than VBA and the Visio shapesheet.
And for someone having mastered the subject of the setref-stuff (you're subjectively the only one in the forum who specialised on this matter) this tiny hurdle should be a joke.... not to speak about the connection-stuff which is almost as "esoteric". ;)
« Last Edit: October 12, 2018, 05:29:28 AM by Yacine »
Yacine