Package: scirisweb
NOTE:While Sciris is still in active development, ScirisWeb is no longer actively maintained. It is still functional, but you may wish to use a more modern Python webapp framework instead, such asShiny for Python,Plotly Dash, orStreamlit.What is Sciris?Glad you asked!Sciris(http://sciris.org) is a library of tools that can make it easier and more pleasant to write scientific Python code. Built on top ofNumPyandMatplotlib, Sciris provides functions covering a wide range of common array and plotting operations. This means you can get more done with less code, and spend less time looking things up on StackOverflow.ScirisWebis an extension of Sciris that allows you to build Python webapps without reinventing the wheel – kind of likeShinyfor Python. In contrast toPlotly DashandStreamlit, which have limited options for customization, ScirisWeb is completely modular, so you have control over which tools to use for which aspects of the project. Out of the box, ScirisWeb provides a “just works” solution usingVuejsfor the frontend,Flaskas the web framework,Redisfor the (optional) database, and Matplotlib/mpld3for plotting. But if you want a React frontend linked to an SQL database with Plotly figures, ScirisWeb can serve as the glue holding all of that together.ScirisWeb is available onPyPi(pip install sciris) andGitHub. Full documentation is available athttp://docs.sciris.org. If you have questions, feature suggestions, or would like some help getting started, please reach out to us atinfo@sciris.org.HighlightsSome highlights of ScirisWeb (import scirisweb as sw):ScirisApp– An extension of a Flask App that can be created as simply asapp = sw.ScirisApp(config)and run withapp.run().RPCs– Simple “remote procedure calls” that define how the frontend (web interface) interacts with the backend (Python server).Datastore– For more fully-featured webapps, user and data management are available based on Redis (with additional options for SQL or file-based databases).Where did Sciris come from?Development of Sciris began in 2014 to support development of theOptimasuite of models. We kept encountering the same issues and inconveniences over and over while building scientific webapps, and began collecting the tools we used to overcome these issues into a shared library. This library evolved into Sciris. (Note: while “Sciris” doesn’t mean anything, “iris” means “rainbow” in Greek, and the name was loosely inspired by the wide spectrum of scientific computing features included in Sciris.)Where has ScirisWeb been used?ScirisWeb is currently used by a number of scientific computing libraries, includingAtomicaandCovasim. ScirisWeb also provides the backend for webapps such as theCascade Analysis Tool,HIPtool, andCovasim.Installation and run instructions5-second quick start guideInstall ScirisWeb:pip install sciriswebUse ScirisWeb:import scirisweb as sw20-second quick start guideDownload ScirisWeb (e.g.git clonehttp://github.com/sciris/scirisweb)Install ScirisWeb (which will install Sciris as well):cd scirisweb; pip install-e.Change to the Hello World folder:cd examples/helloworldRun the app:python app.pyGo tolocalhost:8080in your browserHave fun!Medium-quick start guideNote: if you’re a developer, you’ll likely already have some/all of these packages installed.InstallNodeJS(JavaScript manager)InstallRedis(database)InstallAnaconda Python(scientific Python environment)Clone and install Sciris:git clonehttp://github.com/sciris/scirisClone ScirisWeb:git clonehttp://github.com/sciris/sciriswebOnce you’ve done all that, to install, simply runpip install-e.in the root folders ofscirisandscirisweb. This should install Sciris and ScirisWeb as importable Python modules.To test, open up a new Python window and typeimport sciris(and/orimport scirisweb)If you have problems, please emailinfo@sciris.org, or consult the rest of this guide for more information.Installing on LinuxThe easiest way to install Sciris is by using pip:pip install scirisweb(which will also automatically installsciris). If you want to install from source, follow these steps:Install Git:sudo apt install gitInstall NodeJS:sudo apt install nodejsInstall Redis:https://redis.io/topics/quickstart(Optional) InstallAnaconda Python(as of version 0.15, Sciris is only compatible with Python 3), and make sure it’s the default Python, e.g.your_computer:~> python
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.Clone the Sciris repositories:git clonehttp://github.com/sciris/sciris.gitandgit clonehttp://github.com/sciris/scirisweb.git.Runpip install-e.in each of the two Sciris folders.To test, open up a new Python window and typeimport scirisandimport scirisweb. You should see something like:>>> import sciris
>>> import sciriswebInstalling on WindowsPackage and library dependenciesFirst, make sure that you havenpm(included in Node.js installation) andgitinstalled on your machine.InstallAnaconda Python. In your Python setup, you also need to have the following packages (instructions in parentheses show how to install with Anaconda Python environment already installed).Note, these should all be installed automatically when you typepip install-e.in the Sciris and ScirisWeb folders.Database dependenciesIf you use Redis as your DataStore mode, you will need to have Redis installed on your computer (as a service). Redis does not directly support Windows, but there is aMicrosoftArchive page on GitHubwhere you may go for installation directions on your Windows machine. (For example, it can be installed atthis site, downloading a .msi file). It ends up being installed as a service which you can navigate to by going the Windows Task Manager and going to the Services tab. Make sure theRedisservice is in the Running state.Most likely, the directory for your Redis executables will be installed atC:\ProgramFiles\Redis. In that directory, you can double-click the icon forredis-cli.exeto start the redis database command line interface at the default Redis database (#0). You can dokeys *to look at all of the store key / value pairs in the database, andexitexits the interface.You will probably want to use a non-default (i.e.Nis not 0) database. To investigate what keys are in, for example, database #2, while you are withinredis-cli, you can typeselect 2to switch to that database.Installing on MacInstall Git. This can be done by installing Xcode commandline tools.xcode-select --installInstall NodeJS. Visithttps://nodejs.org/en/download/and download the Mac version and install.Install Redis:https://redis.io/topics/quickstartor run (Assumming brew is installed)brew install redisInstallAnaconda Python 3, and make sure it’s the default Python, e.g.your_computer:~> python
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.Create a directory that will hold Sciris. For reference purposes we will create and refer to that directory aspyenv.Clone the Sciris repository intopyenv:git clonehttp://github.com/sciris/sciris.gitCreate a Python virtual environment (venv) inside the directory of your choice. This will be the parent of the Sciris folder.`virtualenv venv`More information aboutpython virtual environmentscan be foundhere. The project structure should be as follows;-pyenv
    -venv
    -scirisGet into the virtual environment. While inside thepyenvfolder, to activate the virtual environment, type:./venv/bin/activateChange to the Sciris root folder and type:python setup.py developRepeat in the ScirisWeb root folder:python setup.py developTo test if the if everything is working accordingly, open Python window within the virtual environment and typeimport scirisandimport scirisweb. If no errors occur, then the import worked.Multhreaded deploymentThe problem with the simple deployment method described above is that requests are single-threaded. If this is an issue, recommended deployment is usingnginxto serve the static files, andgunicornto run the Flask app. Note that it is common for an application to call several RPCs with each page load. This means that the multithreaded deployment can result in improved site performance even for a single user.RequirementsYou must have nginx (sudo apt install nginx) and gunicorn
(pip install gunicorn) installed.Set up nginxCopyexamples/gunicorn/example_nginx_configto e.g./etc/nginx/sites-enabled/my_app(can change filename if desired)Edit the copied file to specifyThe hostname/URL for the site e.g.my_app.comThe full path to the directory containingindex.htmlon the
system runningnginxChange the port inproxy_passline if desired - it must match
the port inlaunch_gunicornReload or restartnginxe.g.sudo service nginx reloadFor example, this will start it running atlocalhost:8188:server{listen8188;server_namelocalhost;location/{root/home/my_username/my_sciris_app;}location/api{proxy_passhttp://127.0.0.1:8097/;}}Run gunicornCopyexamples/gunicorn/example_launch_gunicornto the folder with your app (e.g.launch_my_app_gunicorn), and set the number of workers as desired - usual recommendation is twice the number of CPUs but for applications that are CPU bound (e.g., an RPC call runs a model) then it may be better to reduce it to just the number of CPUs.The example script references the Flask app usingname_of_your_app:flask_app. Thename_of_your_appshould be importable in Python (either via running Python in the current directory, or installing as a package viapip) andflask_appis the name of a variable containing the Flask application. So for  example, you might have a filefoo.pycontainingapp=sw.ScirisApp(__name__,name="My App")the_app=app.flask_appin which case thelaunch_my_app_gunicornscript should containfoo:the_appinstead ofname_of_your_app:flask_app.Runlaunch_my_app_gunicorn. This will need to be kept running to support the site (so run vianohuporscreenetc.).For example:cdmy_appscreen-Smy_app_session./launch_my_app_gunicorn<youcannowclosetheterminal>...<comingbacklater,youcanrestartitwith>screen-Rmy_app_sessionNote that for local development, you can add the--reloadflag to thegunicorncommand to automatically reload the site. This can be helpful if using thenginx+gunicornsetup for local development.ExamplesIn theexamplesandvue_proto_webappsdirectories are contained a number of working examples of web applications combining Vue, Flask, and Twisted. These are being used as stepping stones for developing the main framework based inuser_interface,session_manager,model_code, andbin.Hello WorldA very simple test case of Sciris. In theexamples/helloworldfolder, typepython app.py. If you go tolocalhost:8080in your browser, it should be running a simple Python webapp.See the directionshereon how to install and run this example.